目录
1.北京市空气质量月度差异
2.成都市空气质量月度差异
3.上海市空气质量月度差异
五城P.M.2.5数据分析与可视化_使用复式柱状图分析各个城市的P.M.2.5月度差异情况
1.北京市空气质量月度差异
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt#读入文件
bj = pd.read_csv('./Beijing.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_dist2def good(pm):#优degree = pm-35for i in range(len(degree)):if degree[i] > 0:degree[i] = 35else:degree[i] += 35return degree
def moderate(pm):#良degree = pm-35for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 40for i in range(len(degree)):if degree[i] > 0:degree[i] = 40else:degree[i] += 40return degree
def lightlyP(pm):#轻度污染degree = pm-75for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 40for i in range(len(degree)):if degree[i] > 0:degree[i] = 40else:degree[i] += 40return degree
def moderatelyP(pm):#中度污染degree = pm - 115for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 35for i in range(len(degree)):if degree[i] > 0:degree[i] = 35else:degree[i] += 35return degree
def heavilyP(pm):#重度污染degree = pm - 150for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 100for i in range(len(degree)):if degree[i] > 0:degree[i] = 100else:degree[i] += 100return degree#北京
#PM_Dongsi列
bj_ds = PM(bj,'month','PM_Dongsi')
PM_Dongsi = np.array(bj_ds['PM_Dongsi'])
PM_Dongsi_good = good(PM_Dongsi)
PM_Dongsi_moderate = moderate(PM_Dongsi)
PM_Dongsi_lightlyP = lightlyP(PM_Dongsi)
PM_Dongsi_moderatelyP = moderatelyP(PM_Dongsi)
PM_Dongsi_heavilyP = heavilyP(PM_Dongsi)
#PM_Dongsihuan列
bj_dsh = PM(bj,'month','PM_Dongsihuan')
PM_Dongsihuan = np.array(bj_dsh['PM_Dongsihuan'])
PM_Dongsihuan_good = good(PM_Dongsihuan)
PM_Dongsihuan_moderate = moderate(PM_Dongsihuan)
PM_Dongsihuan_lightlyP = lightlyP(PM_Dongsihuan)
PM_Dongsihuan_moderatelyP = moderatelyP(PM_Dongsihuan)
PM_Dongsihuan_heavilyP = heavilyP(PM_Dongsihuan)
#PM_Nongzhanguan列
bj_nzg = PM(bj,'month','PM_Nongzhanguan')
PM_Nongzhanguan = np.array(bj_nzg['PM_Nongzhanguan'])
PM_Nongzhanguan_good = good(PM_Nongzhanguan)
PM_Nongzhanguan_moderate = moderate(PM_Nongzhanguan)
PM_Nongzhanguan_lightlyP = lightlyP(PM_Nongzhanguan)
PM_Nongzhanguan_moderatelyP = moderatelyP(PM_Nongzhanguan)
PM_Nongzhanguan_heavilyP = heavilyP(PM_Nongzhanguan)#导出
width = 0.2
y = [0,0,35,75,115,150]
x1 = list(range(1,len(bj_nzg.month)+1))
x2 = [i+width for i in x1]
x3 = [i+width*2 for i in x1]
plt.bar(x1,PM_Dongsi_good,width,color='gold')
plt.bar(x1,PM_Dongsi_moderate,width,bottom=PM_Dongsi_good,color='gold')
plt.bar(x1,PM_Dongsi_lightlyP,width,bottom=PM_Dongsi_moderate+PM_Dongsi_good,color='gold')
p1 = plt.bar(x1,PM_Dongsi_moderatelyP,width,bottom=PM_Dongsi_lightlyP+PM_Dongsi_moderate+PM_Dongsi_good,color='gold')
plt.bar(x2,PM_Dongsihuan_good,width,color='tomato')
plt.bar(x2,PM_Dongsihuan_moderate,width,bottom=PM_Dongsihuan_good,color='tomato')
plt.bar(x2,PM_Dongsihuan_lightlyP,width,bottom=PM_Dongsihuan_moderate+PM_Dongsihuan_good,color='tomato')
p2 = plt.bar(x2,PM_Dongsi_moderatelyP,width,bottom=PM_Dongsihuan_lightlyP+PM_Dongsihuan_moderate+PM_Dongsihuan_good,color='tomato')
plt.bar(x3,PM_Nongzhanguan_good,width,color='peru')
plt.bar(x3,PM_Nongzhanguan_moderate,width,bottom=PM_Nongzhanguan_good,color='peru')
plt.bar(x3,PM_Nongzhanguan_lightlyP,width,bottom=PM_Nongzhanguan_moderate+PM_Nongzhanguan_good,color='peru')
p3 = plt.bar(x3,PM_Nongzhanguan_moderatelyP,width,bottom=PM_Nongzhanguan_lightlyP+PM_Nongzhanguan_moderate+PM_Nongzhanguan_good,color='peru')
plt.xticks(x2,bj_nzg.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],p3[0]),('Dongsi.D','Dongsihuan.D','Nongzhanguan.D'))
plt.grid(alpha=0.4)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.show()
2.成都市空气质量月度差异
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt#读入文件
cd = pd.read_csv('./Chengdu.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_dist2def good(pm):#优degree = pm-35for i in range(len(degree)):if degree[i] > 0:degree[i] = 35else:degree[i] += 35return degree
def moderate(pm):#良degree = pm-35for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 40for i in range(len(degree)):if degree[i] > 0:degree[i] = 40else:degree[i] += 40return degree
def lightlyP(pm):#轻度污染degree = pm-75for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 40for i in range(len(degree)):if degree[i] > 0:degree[i] = 40else:degree[i] += 40return degree
def moderatelyP(pm):#中度污染degree = pm - 115for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 35for i in range(len(degree)):if degree[i] > 0:degree[i] = 35else:degree[i] += 35return degree
def heavilyP(pm):#重度污染degree = pm - 150for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 100for i in range(len(degree)):if degree[i] > 0:degree[i] = 100else:degree[i] += 100return degree#成都
#PM_Caotangsi列
cd_cts = PM(cd,'month','PM_Caotangsi')
PM_Caotangsi = np.array(cd_cts['PM_Caotangsi'])
PM_Caotangsi_good = good(PM_Caotangsi)
PM_Caotangsi_moderate = moderate(PM_Caotangsi)
PM_Caotangsi_lightlyP = lightlyP(PM_Caotangsi)
PM_Caotangsi_moderatelyP = moderatelyP(PM_Caotangsi)
PM_Caotangsi_heavilyP = heavilyP(PM_Caotangsi)
#PM_Shahepu列
cd_shp = PM(cd,'month','PM_Shahepu')
PM_Shahepu = np.array(cd_shp['PM_Shahepu'])
PM_Shahepu_good = good(PM_Shahepu)
PM_Shahepu_moderate = moderate(PM_Shahepu)
PM_Shahepu_lightlyP = lightlyP(PM_Shahepu)
PM_Shahepu_moderatelyP = moderatelyP(PM_Shahepu)
PM_Shahepu_heavilyP = heavilyP(PM_Shahepu)
#导出
width = 0.2
y = [0,0,35,75,115,150]
x1 = list(range(1,len(cd_shp.month)+1))
x2 = [i+width for i in x1]
plt.bar(x1,PM_Caotangsi_good,width,color='gold')
plt.bar(x1,PM_Caotangsi_moderate,width,bottom=PM_Caotangsi_good,color='gold')
plt.bar(x1,PM_Caotangsi_lightlyP,width,bottom=PM_Caotangsi_moderate+PM_Caotangsi_good,color='gold')
p1 = plt.bar(x1,PM_Caotangsi_moderatelyP,width,bottom=PM_Caotangsi_lightlyP+PM_Caotangsi_moderate+PM_Caotangsi_good,color='gold')
plt.bar(x2,PM_Shahepu_good,width,color='tomato')
plt.bar(x2,PM_Shahepu_moderate,width,bottom=PM_Shahepu_good,color='tomato')
plt.bar(x2,PM_Shahepu_lightlyP,width,bottom=PM_Shahepu_moderate+PM_Shahepu_good,color='tomato')
p2 = plt.bar(x2,PM_Shahepu_moderatelyP,width,bottom=PM_Shahepu_lightlyP+PM_Shahepu_moderate+PM_Shahepu_good,color='tomato')
plt.xticks(cd_shp.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]),('Shahepu.D','Caotangsi.D'))
plt.grid(alpha=0.4)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.show()
3.上海市空气质量月度差异
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_dist2def good(pm):#优degree = pm-35for i in range(len(degree)):if degree[i] > 0:degree[i] = 35else:degree[i] += 35return degree
def moderate(pm):#良degree = pm-35for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 40for i in range(len(degree)):if degree[i] > 0:degree[i] = 40else:degree[i] += 40return degree
def lightlyP(pm):#轻度污染degree = pm-75for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 40for i in range(len(degree)):if degree[i] > 0:degree[i] = 40else:degree[i] += 40return degree
def moderatelyP(pm):#中度污染degree = pm - 115for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 35for i in range(len(degree)):if degree[i] > 0:degree[i] = 35else:degree[i] += 35return degree
def heavilyP(pm):#重度污染degree = pm - 150for i in range(len(degree)):if degree[i] < 0:degree[i] = 0degree -= 100for i in range(len(degree)):if degree[i] > 0:degree[i] = 100else:degree[i] += 100return 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()