以年为组画temperature的boxplot

temp_15_df = df_15_oclock[[“date”, “temperature_2m”, “Year”]]
以每年为分组,画boxplot。x轴是年份,y轴是temperature_2m

用matplotlib:

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:

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

加图像大小:

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()
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TuringSnowy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值