目录
4.上海市空气质量月度差异
5.沈阳市空气质量月度差异
五城P.M.2.5数据分析与可视化_使用复式柱状图分析各个城市的P.M.2.5月度差异情况
4.上海市空气质量月度差异
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#读入文件
sh = pd.read_csv('./Shanghai.csv')
fig = plt.figure(dpi=100,figsize=(10,5))
def PM(grade,str2,str3):
grade_dist = grade.loc[:, [str2, str3]]
grade_dist1 = grade_dist.dropna(axis=0, subset=[str3])
grade_dist_pm = grade.loc[:, [str3]]
grade_dist1_pm = grade_dist_pm.dropna(axis=0, subset=[str3])
grade_dist_pm_mean = float(grade_dist1_pm.mean())
grade_dist_pm_std = float(grade_dist1_pm.std())
pm_area = grade_dist1[np.abs(grade_dist1[str3] - grade_dist_pm_mean) <= 3 * grade_dist_pm_std]
grade_dist2 = pm_area.groupby([str2]).mean().reset_index()
return grade_dist2
def good(pm):
#优
degree = pm-35
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 35
else:
degree[i] += 35
return degree
def moderate(pm):
#良
degree = pm-35
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 40
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 40
else:
degree[i] += 40
return degree
def lightlyP(pm):
#轻度污染
degree = pm-75
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 40
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 40
else:
degree[i] += 40
return degree
def moderatelyP(pm):
#中度污染
degree = pm - 115
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 35
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 35
else:
degree[i] += 35
return degree
def heavilyP(pm):
#重度污染
degree = pm - 150
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 100
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 100
else:
degree[i] += 100
return degree
#上海
#PM_Jingan列
sh_jg = PM(sh,'month','PM_Jingan')
PM_Jingan = np.array(sh_jg['PM_Jingan'])
PM_Jingan_good = good(PM_Jingan)
PM_Jingan_moderate = moderate(PM_Jingan)
PM_Jingan_lightlyP = lightlyP(PM_Jingan)
PM_Jingan_moderatelyP = moderatelyP(PM_Jingan)
PM_Jingan_heavilyP = heavilyP(PM_Jingan)
#PM_Xuhui列
sh_xh = PM(sh,'month','PM_Xuhui')
PM_Xuhui = np.array(sh_xh['PM_Xuhui'])
PM_Xuhui_good = good(PM_Xuhui)
PM_Xuhui_moderate = moderate(PM_Xuhui)
PM_Xuhui_lightlyP = lightlyP(PM_Xuhui)
PM_Xuhui_moderatelyP = moderatelyP(PM_Xuhui)
PM_Xuhui_heavilyP = heavilyP(PM_Xuhui)
#导出
width = 0.2
y = [0,0,35,75,115,150]
x1 = list(range(1,len(sh_xh.month)+1))
x2 = [i+width for i in x1]
plt.bar(x1,PM_Jingan_good,width,color='gold')
plt.bar(x1,PM_Jingan_moderate,width,bottom=PM_Jingan_good,color='gold')
plt.bar(x1,PM_Jingan_lightlyP,width,bottom=PM_Jingan_moderate+PM_Jingan_good,color='gold')
p1 = plt.bar(x1,PM_Jingan_moderatelyP,width,bottom=PM_Jingan_lightlyP+PM_Jingan_moderate+PM_Jingan_good,color='gold')
plt.bar(x2,PM_Xuhui_good,width,color='tomato')
plt.bar(x2,PM_Xuhui_moderate,width,bottom=PM_Xuhui_good,color='tomato')
plt.bar(x2,PM_Xuhui_lightlyP,width,bottom=PM_Xuhui_moderate+PM_Xuhui_good,color='tomato')
p2 = plt.bar(x2,PM_Xuhui_moderatelyP,width,bottom=PM_Xuhui_lightlyP+PM_Xuhui_moderate+PM_Xuhui_good,color='tomato')
plt.xticks(sh_xh.month)
plt.title(u"上海市城区空气质量月度差异")
plt.yticks(y,("0","优\n(0~35)","良\n(35~75)","轻度污染\n(75~115)","中度污染\n(115~150)","重度污染\n(150~250)"))
plt.xlabel(u'月份',fontsize=12,verticalalignment='top',horizontalalignment='left', x=1)
plt.ylabel(u'污染程度',rotation='horizontal',fontsize=12,verticalalignment='top',horizontalalignment='left', y=1.1)
plt.legend((p1[0],p2[0]),('Jingan.D','Xuhui.D'))
plt.grid(alpha=0.4)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.show()
5.沈阳市空气质量月度差异
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#读入文件
sy = pd.read_csv('./Shenyang.csv')
fig = plt.figure(dpi=100,figsize=(10,5))
def PM(grade,str2,str3):
grade_dist = grade.loc[:, [str2, str3]]
grade_dist1 = grade_dist.dropna(axis=0, subset=[str3])
grade_dist_pm = grade.loc[:, [str3]]
grade_dist1_pm = grade_dist_pm.dropna(axis=0, subset=[str3])
grade_dist_pm_mean = float(grade_dist1_pm.mean())
grade_dist_pm_std = float(grade_dist1_pm.std())
pm_area = grade_dist1[np.abs(grade_dist1[str3] - grade_dist_pm_mean) <= 3 * grade_dist_pm_std]
grade_dist2 = pm_area.groupby([str2]).mean().reset_index()
return grade_dist2
def good(pm):
#优
degree = pm-35
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 35
else:
degree[i] += 35
return degree
def moderate(pm):
#良
degree = pm-35
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 40
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 40
else:
degree[i] += 40
return degree
def lightlyP(pm):
#轻度污染
degree = pm-75
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 40
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 40
else:
degree[i] += 40
return degree
def moderatelyP(pm):
#中度污染
degree = pm - 115
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 35
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 35
else:
degree[i] += 35
return degree
def heavilyP(pm):
#重度污染
degree = pm - 150
for i in range(len(degree)):
if degree[i] < 0:
degree[i] = 0
degree -= 100
for i in range(len(degree)):
if degree[i] > 0:
degree[i] = 100
else:
degree[i] += 100
return degree
#沈阳
#PM_Taiyuanjie列
sy_tyj = PM(sy,'month','PM_Taiyuanjie')
PM_Taiyuanjie = np.array(sy_tyj['PM_Taiyuanjie'])
PM_Taiyuanjie_good = good(PM_Taiyuanjie)
PM_Taiyuanjie_moderate = moderate(PM_Taiyuanjie)
PM_Taiyuanjie_lightlyP = lightlyP(PM_Taiyuanjie)
PM_Taiyuanjie_moderatelyP = moderatelyP(PM_Taiyuanjie)
PM_Taiyuanjie_heavilyP = heavilyP(PM_Taiyuanjie)
#PM_Xiaoheyan列
sy_xhy = PM(sy,'month','PM_Xiaoheyan')
PM_Xiaoheyan = np.array(sy_xhy['PM_Xiaoheyan'])
PM_Xiaoheyan_good = good(PM_Xiaoheyan)
PM_Xiaoheyan_moderate = moderate(PM_Xiaoheyan)
PM_Xiaoheyan_lightlyP = lightlyP(PM_Xiaoheyan)
PM_Xiaoheyan_moderatelyP = moderatelyP(PM_Xiaoheyan)
PM_Xiaoheyan_heavilyP = heavilyP(PM_Xiaoheyan)
#导出
width = 0.2
y = [0,0,35,75,115,150]
x1 = list(range(1,len(sy_tyj.month)+1))
x2 = [i+width for i in x1]
plt.bar(x1,PM_Taiyuanjie_good,width,color='gold')
plt.bar(x1,PM_Taiyuanjie_moderate,width,bottom=PM_Taiyuanjie_good,color='gold')
plt.bar(x1,PM_Taiyuanjie_lightlyP,width,bottom=PM_Taiyuanjie_moderate+PM_Taiyuanjie_good,color='gold')
p1 = plt.bar(x1,PM_Taiyuanjie_moderatelyP,width,bottom=PM_Taiyuanjie_lightlyP+PM_Taiyuanjie_moderate+PM_Taiyuanjie_good,color='gold')
plt.bar(x2,PM_Xiaoheyan_good,width,color='tomato')
plt.bar(x2,PM_Xiaoheyan_moderate,width,bottom=PM_Xiaoheyan_good,color='tomato')
plt.bar(x2,PM_Xiaoheyan_lightlyP,width,bottom=PM_Xiaoheyan_moderate+PM_Xiaoheyan_good,color='tomato')
p2 = plt.bar(x2,PM_Xiaoheyan_moderatelyP,width,bottom=PM_Xiaoheyan_lightlyP+PM_Xiaoheyan_moderate+PM_Xiaoheyan_good,color='tomato')
plt.xticks(sy_tyj.month)
plt.title(u"沈阳市城区空气质量月度差异")
plt.yticks(y,("0","优\n(0~35)","良\n(35~75)","轻度污染\n(75~115)","中度污染\n(115~150)","重度污染\n(150~250)"))
plt.xlabel(u'月份',fontsize=12,verticalalignment='top',horizontalalignment='left', x=1)
plt.ylabel(u'污染程度',rotation='horizontal',fontsize=12,verticalalignment='top',horizontalalignment='left', y=1.1)
plt.legend((p1[0],p2[0]),('Taiyuanjie.D','Xiaoheyan.D'))
plt.grid(alpha=0.4)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.show()