Python||五城P.M.2.5数据分析与可视化_使用复式柱状图分析各个城市的P.M.2.5月度差异情况(上)

目录

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_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_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_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_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_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()

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小嘤嘤怪学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值