前言
大气空气质量污染物一般为SO2、NO2 、CO、PM10、PM2.5、O3六项污染物。日级别浓度中,除O3使用最大8小时滑动平均浓度外,其他五项污染物均采用日平均浓度。
除CO浓度单位为mg/m3外,其他五项污染物浓度单位均为μg/m3,各污染物浓度对应的级别见《环境空气质量标准》(GB 3095—2012)。
分析流程
对数据进行专题二的预处理后,计算出各污染物各年的年均浓度,最后进行可视化分析。
核心代码
(1)计算各污染物各年年均浓度(需要注意的是,求某指标某时段的均值时,要用该指标该时段的综合除以总记录数,不能分段平均再平均,分段平均再平均只能适用于分段方式为等长度分段)
@staticmethod
def each_year_concentration_cal(dft):
"""
计算某年均浓度
:param dft: 该年全年数据
:return: 该年各污染物浓度年均值
"""
return round(np.mean(dft['PM10']), 2), round(np.mean(dft['PM2.5']), 2), round(np.mean(dft['SO2']), 2), \
round(np.mean(dft['NO2']), 2), round(np.mean(dft['CO']), 2), round(np.mean(dft['O3']), 2)
(2)可视化分析
def year_trend_analysis(self, df_station, year_list):
"""
日级别数据年变化分析
:param df_station: 站点数据
:param year_list: 年份列表
:return: None
"""
all_year_pollutes = list()
for year in year_list:
df_station_year = df_station[df_station['year'] == year]
if len(df_station_year.index) != 0:
all_year_pollutes.append(list(self.each_year_concentration_cal(df_station_year)))
else:
all_year_pollutes.append([np.NAN] * 6)
logger.info(df_station['station'].values[0] + '污染物浓度为: ' + str(all_year_pollutes))
co_arr = list()
for i in range(len(all_year_pollutes)):
co_arr.append(all_year_pollutes[i].pop(4))
bar_width = 0.1
index_pm10 = np.arange(len(all_year_pollutes))
index_pm2_5 = index_pm10 + 1.5 * bar_width
index_so2 = index_pm2_5 + 1.5 * bar_width # 年份INDEX
index_no2 = index_so2 + 1.5 * bar_width
index_o3 = index_no2 + 1.5 * bar_width
x_ticks = [str(x) + "年" for x in year_list]
fig = plt.figure()
ax1 = fig.add_subplot(111)
plt.bar(index_pm10, height=[k[0] for k in all_year_pollutes], width=bar_width, label='PM$_{10}$')
plt.bar(index_pm2_5, height=[k[1] for k in all_year_pollutes], width=bar_width, label='PM$_{2.5}$')
plt.bar(index_so2, height=[k[2] for k in all_year_pollutes], width=bar_width, label='SO$_{2}$')
plt.bar(index_no2, height=[k[3] for k in all_year_pollutes], width=bar_width, label='NO$_{2}$')
plt.bar(index_o3, height=[k[4] for k in all_year_pollutes], width=bar_width, label='O$_{3}$')
ax1.set_ylim(0, 100)
ax1.set_yticks(np.linspace(0, 100, 6))
plt.legend(ncol=1, loc='upper left', fontsize=8) # 显示图例
plt.ylabel('污染物浓度(${μg}$/m${^3}$)') # 纵坐标轴标题
ax2 = ax1.twinx()
ax2.plot(index_pm10 + bar_width * 3, co_arr, label='CO浓度', color='black', marker='o', markerfacecolor='red',
markersize=4)
plt.ylabel('CO浓度(mg/m${^3}$)')
plt.xticks(index_pm10 + bar_width * 3, x_ticks)
plt.legend(loc='upper right', fontsize=8)
ax2.set_yticks(np.linspace(0, 1.5, 6))
plt.grid(axis='y', ls='--')
pic_loc0 = Path(self.cf_info['output']['picture']).joinpath(df_station['city'].values[0])
pic_loc = pic_loc0.joinpath('污染物年变化特征')
if not os.path.exists(pic_loc):
os.mkdir(pic_loc)
plt.savefig(pic_loc / (
df_station['station'].values[0] + str(year_list[0]) + '-' + str(year_list[-1]) + '年各污染物浓度变化.png'),
dpi=1200, bbox_inches='tight')
plt.show()
结果展示与分析
结果如下图所示,从该图可以看出各污染物年均浓度的变化趋势,有利于对该地区空气质量的变化情况有直观了解。

(图片右键新标签页打开会很清晰)
预告
下期进行污染物浓度季节变化的分析。
以下是本人独自运营的微信公众号,用于分享个人学习及工作生活趣事,大佬们可以关注一波。
