以年为组画temperature的boxplot

temp_15_df = df_15_oclock[["date", "temperature_2m", "Year"]]

以每年为分组,画boxplot。x轴是年份,y轴是temperature_2m

用matplotlib:

python 复制代码
import pandas as pd
import matplotlib.pyplot as plt

# 假设df_15_oclock是您已经加载的数据框
# temp_15_df = df_15_oclock[["date", "temperature_2m", "Year"]]

# 首先确保"Year"列是整数类型
temp_15_df['Year'] = temp_15_df['Year'].astype(int)

# 使用Pandas的groupby函数按"Year"分组,并计算每组的描述性统计数据
grouped = temp_15_df.groupby('Year')['temperature_2m']

# 绘制箱型图
plt.figure(figsize=(10, 6))  # 可以调整图形大小
for name, group in grouped:
    plt.boxplot(group, positions=[name], manage_xticks=False)  # 绘制每个年份的箱型图

# 设置x轴的刻度,使其显示年份
plt.xticks(range(temp_15_df['Year'].min(), temp_15_df['Year'].max() + 1))

# 添加标签和标题
plt.xlabel('Year')
plt.ylabel('Temperature (2m)')
plt.title('Boxplot of Temperature by Year')

# 显示图形
plt.show()

用seaborn:

python 复制代码
import seaborn as sns

# 使用Seaborn绘制箱型图
sns.boxplot(x='Year', y='temperature_2m', data=temp_15_df)
plt.xlabel('Year')
plt.ylabel('Temperature (2m)')
plt.title('Boxplot of Temperature by Year')
plt.show()

加图像大小:

python 复制代码
import pandas as pd
import matplotlib.pyplot as plt

# 假设df_15_oclock是您已经加载的数据框
# temp_15_df = df_15_oclock[["date", "temperature_2m", "Year"]]

# 确保'Year'列是整数类型
temp_15_df['Year'] = temp_15_df['Year'].astype(int)

# 使用Pandas的groupby函数按"Year"分组,并绘制箱型图
plt.figure(figsize=(12, 7))  # 设置图像大小为12x7英寸
temp_15_df.boxplot(column='temperature_2m', by='Year')

# 添加x轴和y轴的标签
plt.xlabel('Year')
plt.ylabel('Temperature (2m)')

# 添加标题
plt.title('Boxplot of Temperature by Year')

# 显示图形
plt.show()
相关推荐
紫雾凌寒40 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器
cwtlw9 小时前
PhotoShop学习01
笔记·学习·ui·photoshop
远离UE49 小时前
UE5 Computer Shader学习笔记
笔记·学习·ue5
让学习成为一种生活方式10 小时前
libGL.so.1: cannot open shared object file: No such file or directory-linux022
linux·开发语言·python
java1234_小锋10 小时前
一周学会Flask3 Python Web开发-Jinja2模板继承和include标签使用
python·flask·flask3
图书馆钉子户10 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis