目录
沈阳市的空气质量
华夫图
柱状图
总结
五城P.M.2.5数据分析与可视化——北京市、上海市、广州市、沈阳市、成都市,使用华夫图和柱状图分析各个城市的情况
沈阳市的空气质量
华夫图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pywaffle import Waffle
#读入文件
sy = pd.read_csv('./Shenyang.csv')
fig = plt.figure(dpi=100,figsize=(5,5))def good(pm):#优degree = []for i in pm:if 0 < i <= 35:degree.append(i)return degree
def moderate(pm):#良degree = []for i in pm:if 35 < i <= 75:degree.append(i)return degree
def lightlyP(pm):#轻度污染degree = []for i in pm:if 75 < i <= 115:degree.append(i)return degree
def moderatelyP(pm):#中度污染degree = []for i in pm:if 115 < i <= 150:degree.append(i)return degree
def heavilyP(pm):#重度污染degree = []for i in pm:if 150 < i <= 250:degree.append(i)return degree
def severelyP(pm):#严重污染degree = []for i in pm:if 250 < i:degree.append(i)return degreedef PM(sy,str3):sy_dist_pm = sy.loc[:, [str3]]sy_dist1_pm = sy_dist_pm.dropna(axis=0, subset=[str3])sy_dist1_pm = np.array(sy_dist1_pm[str3])sy_good_count = len(good(sy_dist1_pm))sy_moderate_count = len(moderate(sy_dist1_pm))sy_lightlyP_count = len(lightlyP(sy_dist1_pm))sy_moderatelyP_count = len(moderatelyP(sy_dist1_pm))sy_heavilyP_count = len(heavilyP(sy_dist1_pm))sy_severelyP_count = len(severelyP(sy_dist1_pm))a = {'优':sy_good_count,'良':sy_moderate_count,'轻度污染':sy_lightlyP_count,'中度污染':sy_moderatelyP_count,'重度污染':sy_heavilyP_count,'严重污染':sy_severelyP_count}pm = pd.DataFrame(pd.Series(a),columns=['daysum'])pm = pm.reset_index().rename(columns={'index':'level'})return pm
#沈阳
#PM_Taiyuanjie列
sy_tyj = PM(sy,'PM_Taiyuanjie')
PMday_Taiyuanjie = np.array(sy_tyj['daysum'])
#PM_Xiaoheyan列
sy_xhy = PM(sy,'PM_Xiaoheyan')
PMday_Xiaoheyan = np.array(sy_xhy['daysum'])
sy_pm_daysum = (PMday_Xiaoheyan+PMday_Taiyuanjie)/2
sum = 0
for i in sy_pm_daysum:sum += i
sy_pm_daysum1 = np.array(sy_pm_daysum)
data = {'优':int((sy_pm_daysum[0]/sum)*100), '良':int((sy_pm_daysum[1]/sum)*100), '轻度污染': int(sy_pm_daysum[2]/sum*100),'中度污染':int((sy_pm_daysum[3]/sum)*100),'重度污染':int((sy_pm_daysum[4]/sum)*100),'严重污染':int((sy_pm_daysum[5]/sum)*100)}
total = np.sum(list(data.values()))
plt.figure(FigureClass=Waffle,rows = 5, # 列数自动调整values = data,# 设置titletitle = {'label': "沈阳市污染情况",'loc': 'center','fontdict':{'fontsize': 13,}},labels = ['{} {:.1f}%'.format(k, (v/total*100)) for k, v in data.items()],# 设置标签图例的样式legend = {'loc': 'lower left','bbox_to_anchor': (0, -0.4),'ncol': len(data),'framealpha': 0,'fontsize': 6},dpi=120
)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.show()
柱状图
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=(5,5))def good(pm):#优degree = []for i in pm:if 0 < i <= 35:degree.append(i)return degree
def moderate(pm):#良degree = []for i in pm:if 35 < i <= 75:degree.append(i)return degree
def lightlyP(pm):#轻度污染degree = []for i in pm:if 75 < i <= 115:degree.append(i)return degree
def moderatelyP(pm):#中度污染degree = []for i in pm:if 115 < i <= 150:degree.append(i)return degree
def heavilyP(pm):#重度污染degree = []for i in pm:if 150 < i <= 250:degree.append(i)return degree
def severelyP(pm):#严重污染degree = []for i in pm:if 250 < i:degree.append(i)return degreedef PM(sy,str3):sy_dist_pm = sy.loc[:, [str3]]sy_dist1_pm = sy_dist_pm.dropna(axis=0, subset=[str3])sy_dist1_pm = np.array(sy_dist1_pm[str3])sy_good_count = len(good(sy_dist1_pm))sy_moderate_count = len(moderate(sy_dist1_pm))sy_lightlyP_count = len(lightlyP(sy_dist1_pm))sy_moderatelyP_count = len(moderatelyP(sy_dist1_pm))sy_heavilyP_count = len(heavilyP(sy_dist1_pm))sy_severelyP_count = len(severelyP(sy_dist1_pm))a = {'优':sy_good_count,'良':sy_moderate_count,'轻度污染':sy_lightlyP_count,'中度污染':sy_moderatelyP_count,'重度污染':sy_heavilyP_count,'严重污染':sy_severelyP_count}pm = pd.DataFrame(pd.Series(a),columns=['daysum'])pm = pm.reset_index().rename(columns={'index':'level'})return pm
#沈阳
#PM_Taiyuanjie列
sy_tyj = PM(sy,'PM_Taiyuanjie')
PMday_Taiyuanjie = np.array(sy_tyj['daysum'])
#PM_Xiaoheyan列
sy_xhy = PM(sy,'PM_Xiaoheyan')
PMday_Xiaoheyan = np.array(sy_xhy['daysum'])
sy_pm_daysum = (PMday_Xiaoheyan+PMday_Taiyuanjie)/2
sum = 0
for i in sy_pm_daysum:sum += i
sy_pm_daysum1 = np.array(sy_pm_daysum)
#图像
bar_width = 0.1
plt.bar(0.2,sy_pm_daysum[0]/sum,width=bar_width,color='aqua',label='优')
plt.bar(0.4,sy_pm_daysum[1]/sum,width=bar_width,color='deepskyblue',label='良')
plt.bar(0.6,sy_pm_daysum[2]/sum,width=bar_width,color='cornflowerblue',label='轻度污染')
plt.bar(0.8,sy_pm_daysum[3]/sum,width=bar_width,color='skyblue',label='中度污染')
plt.bar(1,sy_pm_daysum[4]/sum,width=bar_width,color='lightsteelblue',label='重度污染')
plt.bar(1.2,sy_pm_daysum[5]/sum,width=bar_width,color='silver',label='严重污染')
x = [0.2,0.4,0.6,0.8,1,1.2]
for a,b in zip(x,sy_pm_daysum):plt.text(a, (b/sum) + 0.02,'%.1f'%(b/sum*100)+'%', ha='center', va='bottom', fontsize=10)
plt.xticks([0.2,0.4,0.6,0.8,1,1.2])
plt.xlabel(['优','良','轻度污染','中度污染','重度污染','严重污染'])
plt.yticks([0.2,0.4,0.6,0.8,1])
plt.ylabel(['20%','40%','60%','80%','100%'])
plt.ylabel(u'百分比',fontsize=12,rotation='horizontal',verticalalignment='top',horizontalalignment='left', x=2,y=1.1)
plt.xlabel(u'污染程度',fontsize=12,verticalalignment='top',horizontalalignment='left',x=0.9, y=1.2)
plt.legend()
fig.suptitle('沈阳市污染情况',fontsize=15,x=0.5,y=1)
plt.grid(alpha=0.4)
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.show()
沈阳市总体空气质量较差,空气污染程度占比超过35%——其中轻度污染占比约16%,中度污染占比约7%,重度污染占比约8%,严重污染占比约3%。
总结
总体来讲,广州市的空气质量最好,上海次之;北京市的空气质量最差,严重污染占比远远超过其他四座城市。