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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/451341.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小白Linux学习笔记-Linux文件系统和磁盘管理

Linux文件系统和磁盘管理 文章目录 Linux文件系统和磁盘管理文件系统资源虚拟化文件系统的概念文件系统的类型文件系统的结构文件系统的区别文件系统的简单操作dfdu 磁盘的分割、格式化与挂载分割 fdisk磁盘格式化 mkfs挂载mount 的用法mount 的查看umount /etc/fstab 将永久生…

ReactNative实现宽度变化实现的动画效果

效果如上图所示,通过修改设备宽度实现动画效果 import React, {useRef, useEffect, useState} from react; import {Animated, Text, View, Image} from react-native;const FadeInView = props => {const fadeAnim = useRef(new Animated.Value(0)).current;React.useEff…

基于YOLOv8算法的照片角度分类项目实践

目录 一、任务概述二、YOLOv8算法简介2.1 算法改进2.2 算法特点2.3 网络结构2.4 性能比较 三、工程实践3.1 安装算法框架库ultralytics3.2 库存照片预处理3.2.1 提取所有图片3.2.2 去除冗余的相同照片3.2.3 去除无车辆照片3.2.4 随机提取指定数量的图片 3.3 照片朝向分类3.3.1 …

PyQT——蓝牙收发数据(上位机案例-小车控制器)

实现功能 由于本人水平有限&#xff0c;仅用了最简单的进行实现&#xff0c;主要功能&#xff1a; 蓝牙设备扫描以及刷新蓝牙连接蓝牙数据发送蓝牙数据接收 页面实现效果 代码目录结构 代码案例 代码已经全部添加注释&#xff0c;故不再做单独解释。 Main.py ble_contr…

Open CASCADE学习|拓扑变换

目录 平移变换 旋转变换 组合变换 通用变换 平移变换 TopoDS_Shape out;gp_Trsf theTransformation;gp_Vec theVectorOfTranslation(0., 0.125 / 2, 0.);theTransformation.SetTranslation(theVectorOfTranslation);BRepBuilderAPI_Transform myBRepTransformation(out, th…

C++继承_C++回顾

什么是继承 继承是面向对象中使代码可以复用的最重要的手段&#xff0c;它允许在保持原有类特性的基础上进行扩展&#xff0c;增加功能&#xff0c;这样就会产生新的类&#xff0c;即派生类。继承呈现了面向对象的层次结构&#xff0c;体现了由简单到复杂的认知过程。与函数复…

MySQL运维实战(5.3) MySQL数据乱码的一些情况

作者&#xff1a;俊达 表数据乱码 表数据出现乱码的情况通常是由于数据的真实编码与相关参数不一致引起的&#xff0c;其中包括常见的参数如character_set_client、character_set_results、字段编码以及终端编码等。确保这些参数保持一致&#xff0c;可以有效预防和解决乱码问…

vit细粒度图像分类(十)TransFG学习笔记

1.摘要 细粒度视觉分类(FGVC)是一项非常具有挑战性的任务&#xff0c;它旨在从子类别中识别对象&#xff0c;这是由于类间固有的微妙差异。现有的大部分工作主要是通过重用骨干网络提取检测到的判别区域的特征来解决这一问题。然而&#xff0c;这种策略不可避免地使管道变得复…

git 如何修改仓库地址

问题背景&#xff1a;组内更换大部门之后&#xff0c;代码仓的地址也迁移了&#xff0c;所以原来的git仓库地址失效了。 虽然重新建一个新的文件夹&#xff0c;再把每个项目都git clone一遍也可以。但是有点繁琐&#xff0c;而且有的项目本地还有已经开发一半的代码&#xff0c…

网络原理-TCP/IP(5)

TCP协议 延迟应答 它也是基于滑动窗口,提高效率的一种机制,结合滑动窗口以及流量控制,能够以延迟应答ACK的方式,把反馈的窗口,搞大.核心在于允许范围内,让窗口尽可能大. 如果接收数据的主机立刻返回ACK应答,这时候返回的窗口可能比较小. 1.假设接收端缓冲区为1M.一次收到了5…

centos 7.6 安装cas 对接ldap 单点登录实战

centos 7.6 安装cas 对ldap 单点登录实战 1、安装前准备工作1.1、centos 7.6 安装JDK 1.81.2、centos 7 安装tomcat 9.0.841.3、windows10 安装JDK 1.81.4、windows10 安装打包工具 maven 3.9.6 2、下载cas 5.3 并打包成war包3、部署cas到tomcat4、centos 7.6 安装ldap5、cas对…

天津政采入围流程?

天津政采入围流程如下&#xff1a; 企业资料提交&#xff1a;申请企业需要提交相关的企业资料&#xff0c;包括企业营业执照、税务登记证、组织机构代码证等。这些资料需要提交给天津政采中心进行审核。 自营商城资料提交&#xff1a;申请企业需要提交自营商城的资料&#xff0…