import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
beijing_data = pd.read_csv(r"D:\PM2.5data\BeijingPM20100101_20151231.csv")
chengdu_data = pd.read_csv(r"D:\PM2.5data\ChengduPM20100101_20151231.csv")
guangzhou_data = pd.read_csv(r"D:\PM2.5data\GuangzhouPM20100101_20151231.csv")
shanghai_data = pd.read_csv(r"D:\PM2.5data\ShanghaiPM20100101_20151231.csv")
shenyang_data = pd.read_csv(r"D:\PM2.5data\ShenyangPM20100101_20151231.csv")
# 设置Matplotlib的字体为"SimHei"以支持中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
city_data_list = [beijing_data, shanghai_data, guangzhou_data, chengdu_data, shenyang_data] # 假设您的五个城市数据集已经被加载
beijing_data['PM2.5'] =beijing_data[['PM_Dongsi', 'PM_Dongsihuan', 'PM_Nongzhanguan']].mean(axis=1)
shanghai_data['PM2.5'] =shanghai_data[['PM_Jingan', 'PM_Xuhui']].mean(axis=1)
guangzhou_data['PM2.5'] =guangzhou_data[['PM_City Station','PM_5th Middle School']].mean(axis=1)
chengdu_data['PM2.5'] =chengdu_data[['PM_Caotangsi','PM_Shahepu']].mean(axis=1)
shenyang_data['PM2.5'] =shenyang_data[['PM_Taiyuanjie','PM_Xiaoheyan']].mean(axis=1)
data_city1=beijing_data.groupby(['year', 'month', 'day'])['PM2.5'].mean().reset_index()
data_city2=shanghai_data.groupby(['year', 'month', 'day'])['PM2.5'].mean().reset_index()
data_city3=guangzhou_data.groupby(['year', 'month', 'day'])['PM2.5'].mean().reset_index()
data_city4=chengdu_data.groupby(['year', 'month', 'day'])['PM2.5'].mean().reset_index()
data_city5=shenyang_data.groupby(['year', 'month', 'day'])['PM2.5'].mean().reset_index()
data_city1_us=beijing_data.groupby(['year', 'month', 'day'])['PM_US Post'].mean().reset_index()
data_city2_us=shanghai_data.groupby(['year', 'month', 'day'])['PM_US Post'].mean().reset_index()
data_city3_us=guangzhou_data.groupby(['year', 'month', 'day'])['PM_US Post'].mean().reset_index()
data_city4_us=chengdu_data.groupby(['year', 'month', 'day'])['PM_US Post'].mean().reset_index()
data_city5_us=shenyang_data.groupby(['year', 'month', 'day'])['PM_US Post'].mean().reset_index()
data_cities_us=[data_city1_us,data_city2_us,data_city3_us,data_city4_us,data_city5_us]
avg_us = []
for data in city_data_list:
daily_avg = data.groupby(['year', 'month', 'day'])['PM_US Post'].mean()
overall_avg = daily_avg.mean()
avg_us.append(overall_avg)
cities_data =[data_city1, data_city2, data_city3, data_city4, data_city5]
city_names = ["北京","上海","广州","成都","沈阳"]
avg_pm25 = [data['PM2.5'].mean() for data in cities_data]
exceed_days = [data[data['PM2.5'] > 75].shape[0] for data in cities_data]
exceed_days_us = [data[data['PM_US Post'] > 75].shape[0] for data in data_cities_us]
# 设定颜色方案
bar_colors = ['#08306b', '#08519c', '#2171b5', '#4292c6', '#6baed6'] # 从深蓝到浅蓝的渐变色
background_color = '#f7fbff' # 轻柔的背景颜色
# 创建2x1的布局,调整figsize的参数
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10, 8))
fig.patch.set_facecolor(background_color) # 设置背景颜色
for ax in axes:
ax.set_facecolor(background_color) # 设置子图的背景颜色
ax.spines['top'].set_color('gray')
ax.spines['right'].set_color('gray')
ax.spines['bottom'].set_color('gray')
ax.spines['left'].set_color('gray')
# 在第一个子图中绘制五个城市的PM2.5平均数值
bars1 = axes[0].bar(city_names, avg_pm25, color=bar_colors, width=0.6) # 使用渐变色
for bar, value in zip(bars1, avg_pm25):
height = bar.get_height()
axes[0].text(bar.get_x() + bar.get_width() / 2, height / 2, f'{value:.2f}', ha='center', color='black', fontsize=10) # 调整字体大小
for bar, days in zip(bars1, exceed_days):
height = bar.get_height()
axes[0].text(bar.get_x() + bar.get_width() / 2, height + 2, f'超标天数: {int(days)}', ha='center', fontsize=10) # 调整字体大小
axes[0].axhline(y=75, color='r', linestyle='--')
axes[0].set_xlabel('城市', fontsize=12)
axes[0].set_ylabel('PM2.5 平均数值', fontsize=12)
axes[0].set_title('五个城市的日PM2.5平均数值及超标天数', fontsize=14)
# 在第二个子图中绘制美国观测的各城市平均数值
bars2 = axes[1].bar(city_names, avg_us, color=bar_colors, width=0.6) # 使用渐变色
for bar, value in zip(bars2, avg_us):
height = bar.get_height()
axes[1].text(bar.get_x() + bar.get_width() / 2, height / 2, f'{value:.2f}', ha='center', color='black', fontsize=10) # 调整字体大小
for bar, days in zip(bars2, exceed_days_us):
height = bar.get_height()
axes[1].text(bar.get_x() + bar.get_width() / 2, height + 2, f'超标天数: {int(days)}', ha='center', fontsize=10) # 调整字体大小
axes[1].axhline(y=75, color='r', linestyle='--')
axes[1].set_xlabel('城市', fontsize=12)
axes[1].set_ylabel('PM2.5 平均数值 (美国观测)', fontsize=12)
axes[1].set_title('美国观测的各城市日PM2.5平均数值及超标天数', fontsize=14)
plt.tight_layout()
plt.show()
Frank牛蛙
- 粉丝: 900
- 资源: 6
最新资源
- springboot基于hive旅游数据的分析与应用4x1c2--论文.zip
- 195ssm饭店餐饮管理系统hsg6034AHA5程序.rar
- 基于python计算两文档间四大文本相似性指标源码-支持中文和英文文本的相似性分析.zip
- springboot289基于JAVA的房地产销售管理系统的设计与实现.rar
- An introduction to Numpy and Scipy.pdf
- 保护Windows台式机的安全措施及其实施方法
- springboot540公司资产网站--论文pf.zip
- 常用的java系统运行环境
- 一个简易的FTP服务器,在ubuntu的vim上使用C语言写成 .zip
- ssm足球赛会管理系统.zip
- 506旧物置换网站.zip
- weixin210微信小程序自助点餐系统springboot.rar
- numpy-learning-resources.pdf
- 基于stm32和mpu9250的usb hid键盘鼠标游戏控制器.zip
- springboot328在线骑行网站.zip
- 基于SpringMVCSpringMyBatis开发的个人博客网站使用IDEA工具开发毕业设计项目资源F.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈