当前位置: 首页 > article >正文

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

目录

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


http://www.kler.cn/a/227982.html

相关文章:

  • 深入解析 C++17 中的 std::not_fn
  • C++:多继承习题5
  • 低代码产品插件功能一览
  • 力扣【1049. 最后一块石头的重量 II】Java题解(背包问题)
  • 【C++ 真题】P1706 全排列问题
  • cursor软件的chat和composer分别是什么
  • Java学习笔记2024/2/3
  • 您应该使用哪个库?jsoniter、easyjson 还是 go-json?为什么?
  • Android电动汽车充电服务vue+uniAPP微信小程序
  • docker初级问题一
  • Vite+Vue3使用Vue-i18n笔记
  • go消息队列RabbitMQ - 订阅模式-direct
  • 万户 ezOFFICE wpsservlet SQL注入漏洞
  • 2024/2/4 备战蓝桥杯 5-1 前缀和
  • 什么是 Java 中的 IO 和 NIO?它们之间有什么区别?什么是 Java 中的内存管理和垃圾回收?常见的垃圾回收算法有哪些?
  • Redisson看门狗机制
  • ubuntu 18.04修改网卡名称
  • 论文阅读-在分布式数据库环境中对哈希算法进行负载均衡基准测试
  • 调试OpenHarmony应用/服务
  • gpt今日最新新闻:gpts的广泛应用
  • Mysql架构系列——生产常用的高可用部署模式介绍
  • 程序执行内存区域,堆栈使用及区别
  • 如何在 Microsoft Azure 上部署和管理 Elastic Stack
  • 开源软件的影响力
  • 【数据结构与算法】(5)基础数据结构之队列 链表实现、环形数组实现详细代码示例讲解
  • Leetcode 3027. Find the Number of Ways to Place People II