以年为组画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()
相关推荐
老歌老听老掉牙10 分钟前
解决 PyQt5 中 sipPyTypeDict() 弃用警告的完整指南
python·qt
武陵悭臾11 分钟前
Python应用开发学习: Pygame 中实现数字水平靠右对齐和垂直靠底对齐
python·学习·程序人生·游戏·个人开发·学习方法·pygame
兜有米啦15 分钟前
python练习题3
开发语言·python
songyuc34 分钟前
《A Bilateral CFAR Algorithm for Ship Detection in SAR Images》译读笔记
人工智能·笔记·计算机视觉
01100001乄夵1 小时前
第二课:时序逻辑入门-零基础FPGA闯关教程
经验分享·笔记·学习方法
你才是向阳花1 小时前
如何用Python实现飞机大战小游戏
开发语言·python·pygame
草莓熊Lotso1 小时前
C++ 方向 Web 自动化测试实战:以博客系统为例,从用例到报告全流程解析
前端·网络·c++·人工智能·后端·python·功能测试
程序员爱钓鱼2 小时前
Python编程实战——Python实用工具与库:Pandas数据处理
后端·python·ipython
程序员爱钓鱼2 小时前
Python编程实战——Python实用工具与库:Numpy基础
后端·python·面试
程序员霸哥哥2 小时前
从零搭建PyTorch计算机视觉模型
人工智能·pytorch·python·计算机视觉