以年为组画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()
相关推荐
Albert Edison5 小时前
【Python】学生管理系统
开发语言·数据库·python
AomanHao6 小时前
【阅读笔记】沙尘图像线性颜色校正A fusion-based enhancing approach for single sandstorm image
图像处理·笔记·isp·图像增强·沙尘图像·色偏·颜色校正
love530love7 小时前
【ComfyUI】解决 ModuleNotFoundError: No module named ‘inference_core_nodes‘ 问题
人工智能·windows·python·comfyui·inference-core
枷锁—sha8 小时前
【pwn系列】Pwndbg 汇编调试实操教程
网络·汇编·笔记·安全·网络安全
亚亚的学习和分享9 小时前
python基础语法----条件语句
python
山岚的运维笔记9 小时前
SQL Server笔记 -- 第73章:排序/对行进行排序
数据库·笔记·后端·sql·microsoft·sqlserver
Zzz 小生10 小时前
LangChain Streaming-Overview:流式处理使用完全指南
人工智能·python·语言模型·langchain·github
yzx99101310 小时前
Python数据结构入门指南:从基础到实践
开发语言·数据结构·python
百锦再10 小时前
Jenkins 全面精通指南:从入门到脚本大师
运维·后端·python·servlet·django·flask·jenkins
FYKJ_201010 小时前
springboot大学校园论坛管理系统--附源码42669
java·javascript·spring boot·python·spark·django·php