{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-30T06:40:32.138260Z",
     "start_time": "2025-04-30T06:40:32.136555Z"
    }
   },
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import matplotlib as mpl\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "\n",
    "# 忽略所有警告\n",
    "warnings.filterwarnings(\"ignore\")"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T06:40:32.170204Z",
     "start_time": "2025-04-30T06:40:32.161366Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置中文字体（macOS 上常见中文字体）\n",
    "from matplotlib import font_manager as fm\n",
    "\n",
    "font_path = '/System/Library/Fonts/STHeiti Medium.ttc'\n",
    "my_font = fm.FontProperties(fname=font_path)\n",
    "mpl.rcParams['font.family'] = my_font.get_name()\n",
    "mpl.rcParams['axes.unicode_minus'] = False"
   ],
   "id": "cf27cc8494b7058",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T06:41:28.147681Z",
     "start_time": "2025-04-30T06:41:28.107064Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = pd.read_csv('./data/aqi.csv')\n",
    "data.head(3)"
   ],
   "id": "bca3f037ac5a1256",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         日期  AQI  质量等级  PM2.5含量（ppm）  PM10含量（ppm）  SO2含量（ppm）  CO含量（ppm）  \\\n",
       "0  2020/1/1   79     良            58           64           8        0.7   \n",
       "1  2020/1/2  112  轻度污染            84           73          10        1.0   \n",
       "2  2020/1/3   68     良            49           51           7        0.8   \n",
       "\n",
       "   NO2含量（ppm）  O3_8h含量（ppm）  \n",
       "0          57            23  \n",
       "1          71             7  \n",
       "2          49             3  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>AQI</th>\n",
       "      <th>质量等级</th>\n",
       "      <th>PM2.5含量（ppm）</th>\n",
       "      <th>PM10含量（ppm）</th>\n",
       "      <th>SO2含量（ppm）</th>\n",
       "      <th>CO含量（ppm）</th>\n",
       "      <th>NO2含量（ppm）</th>\n",
       "      <th>O3_8h含量（ppm）</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020/1/1</td>\n",
       "      <td>79</td>\n",
       "      <td>良</td>\n",
       "      <td>58</td>\n",
       "      <td>64</td>\n",
       "      <td>8</td>\n",
       "      <td>0.7</td>\n",
       "      <td>57</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020/1/2</td>\n",
       "      <td>112</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>84</td>\n",
       "      <td>73</td>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>71</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020/1/3</td>\n",
       "      <td>68</td>\n",
       "      <td>良</td>\n",
       "      <td>49</td>\n",
       "      <td>51</td>\n",
       "      <td>7</td>\n",
       "      <td>0.8</td>\n",
       "      <td>49</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T06:48:20.140741Z",
     "start_time": "2025-04-30T06:48:20.044195Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置图形的大小为宽度 10 英寸，高度 6 英寸，方便后续展示合适大小的图形\n",
    "plt.figure(figsize=(10, 6))\n",
    "# 使用 seaborn 库的 stripplot 函数绘制分类散点图\n",
    "# x 参数指定 x 轴的数据列名为 '质量等级'\n",
    "# y 参数指定 y 轴的数据列名为 'AQI'\n",
    "# data 参数指定使用的数据集为之前读取的 data\n",
    "sns.stripplot(x='质量等级', y='AQI', data=data)\n",
    "# 设置图形的标题为 '质量等级分类散点图'，并使用之前设置好的中文字体\n",
    "plt.title('质量等级分类散点图', fontproperties=my_font)\n",
    "# 设置 x 轴的标签为 '质量等级'，并使用之前设置好的中文字体\n",
    "plt.xlabel('质量等级', fontproperties=my_font)\n",
    "# 设置 y 轴的标签为 'AQI'，并使用之前设置好的中文字体\n",
    "plt.ylabel('AQI', fontproperties=my_font)\n",
    "# 显示绘制好的图形\n",
    "plt.show()"
   ],
   "id": "813907875909cb5e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ],
      "image/png": "[encoded data, 26544 characters]\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T06:50:13.086292Z",
     "start_time": "2025-04-30T06:50:12.949773Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置图形的大小为宽度 10 英寸，高度 6 英寸，便于图形的展示\n",
    "plt.figure(figsize=(10, 6))\n",
    "# 使用 seaborn 库的 regplot 函数绘制线性回归拟合图\n",
    "# x 参数指定 x 轴的数据列名为 'PM2.5含量（ppm）'\n",
    "# y 参数指定 y 轴的数据列名为 'AQI'\n",
    "# data 参数指定使用的数据集为之前读取的 data\n",
    "sns.regplot(x='PM2.5含量（ppm）', y='AQI', data=data)\n",
    "# 设置图形的标题为 'PM2.5 浓度与 AQI 线性回归拟合图'，并使用之前设置好的中文字体\n",
    "plt.title('PM2.5 浓度与 AQI 线性回归拟合图', fontproperties=my_font)\n",
    "# 设置 x 轴的标签为 'PM2.5 含量（ppm）'，并使用之前设置好的中文字体\n",
    "plt.xlabel('PM2.5 含量（ppm）', fontproperties=my_font)\n",
    "# 设置 y 轴的标签为 'AQI'，并使用之前设置好的中文字体\n",
    "plt.ylabel('AQI', fontproperties=my_font)\n",
    "# 显示绘制好的图形\n",
    "plt.show()"
   ],
   "id": "7988481bbda707c4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ],
      "image/png": "[encoded data, 48500 characters]\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T06:52:27.902555Z",
     "start_time": "2025-04-30T06:52:27.884602Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 选取需要计算相关系数的列，包括 AQI 和各空气质量指标列\n",
    "selected_columns = ['AQI', 'PM2.5含量（ppm）', 'PM10含量（ppm）', 'SO2含量（ppm）', 'CO含量（ppm）', 'NO2含量（ppm）',\n",
    "                    'O3_8h含量（ppm）']\n",
    "# 使用 pandas 的 corr() 方法计算这些列之间的相关系数\n",
    "correlation = data[selected_columns].corr()\n",
    "# 打印计算得到的相关系数矩阵\n",
    "print(correlation)"
   ],
   "id": "3a732093dd5b47ae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   AQI  PM2.5含量（ppm）  PM10含量（ppm）  SO2含量（ppm）  CO含量（ppm）  \\\n",
      "AQI           1.000000      0.722789     0.753629    0.481377   0.444786   \n",
      "PM2.5含量（ppm）  0.722789      1.000000     0.760575    0.259824   0.519367   \n",
      "PM10含量（ppm）   0.753629      0.760575     1.000000    0.623885   0.429214   \n",
      "SO2含量（ppm）    0.481377      0.259824     0.623885    1.000000   0.391106   \n",
      "CO含量（ppm）     0.444786      0.519367     0.429214    0.391106   1.000000   \n",
      "NO2含量（ppm）    0.449114      0.489421     0.656500    0.614224   0.494028   \n",
      "O3_8h含量（ppm）  0.461272     -0.123367     0.299201    0.472670  -0.065683   \n",
      "\n",
      "              NO2含量（ppm）  O3_8h含量（ppm）  \n",
      "AQI             0.449114      0.461272  \n",
      "PM2.5含量（ppm）    0.489421     -0.123367  \n",
      "PM10含量（ppm）     0.656500      0.299201  \n",
      "SO2含量（ppm）      0.614224      0.472670  \n",
      "CO含量（ppm）       0.494028     -0.065683  \n",
      "NO2含量（ppm）      1.000000      0.025897  \n",
      "O3_8h含量（ppm）    0.025897      1.000000  \n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T06:57:12.246199Z",
     "start_time": "2025-04-30T06:57:12.044099Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个新的图形窗口，设置图形大小为宽 10 英寸，高 8 英寸\n",
    "plt.figure(figsize=(10, 8))\n",
    "# 使用 seaborn 库的 heatmap 函数绘制热力图\n",
    "# data 参数传入之前计算得到的相关系数矩阵 correlation\n",
    "# annot=True 表示在热力图上显示具体的相关系数数值\n",
    "# cmap='coolwarm' 指定使用的颜色映射，这里使用冷暖色调\n",
    "sns.heatmap(data=correlation, annot=True, cmap='coolwarm')\n",
    "# 设置图形的标题为 '空气质量特征相关性热力图'，并使用之前设置的中文字体\n",
    "plt.title('空气质量特征相关性热力图', fontproperties=my_font)\n",
    "# 显示绘制好的图形\n",
    "plt.show()"
   ],
   "id": "9b2155c3ba022c39",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 720x576 with 2 Axes>"
      ],
      "image/png": "[encoded data, 59668 characters]\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 8
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
