以年为组画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()
相关推荐
codeyanwu18 分钟前
SQL 学习笔记
笔记·sql·学习
wshlp12345634 分钟前
deepseek api 灵活使用
python
71-337 分钟前
C语言——函数声明、定义、调用
c语言·笔记·学习·其他
AI视觉网奇1 小时前
coco json 分类标注工具源代码
开发语言·python
要加油GW2 小时前
python使用vscode 需要配置全局的环境变量。
开发语言·vscode·python
B站计算机毕业设计之家2 小时前
python图像识别系统 AI多功能图像识别检测系统(11种识别功能)银行卡、植物、动物、通用票据、营业执照、身份证、车牌号、驾驶证、行驶证、车型、Logo✅
大数据·开发语言·人工智能·python·图像识别·1024程序员节·识别
报错小能手2 小时前
C++笔记(面向对象)多态(编译时 运行时)
c++·笔记
快乐的钢镚子2 小时前
思腾合力云服务器远程连接
运维·服务器·python
苏打水com2 小时前
爬虫进阶实战:突破动态反爬,高效采集CSDN博客详情页数据
爬虫·python
夫唯不争,故无尤也3 小时前
三大AI部署框架对比:本地权重与多模型协作实战
人工智能·python·深度学习