You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pandas从日期提取月份绘制区域平均产量分组条形图

区域-月度平均产量条形图实现方案

数据预处理

先处理日期列并计算分组均值:

import pandas as pd

# 读取数据(根据实际文件格式调整,如Excel用pd.read_excel)
df = pd.read_csv("production_data.csv")

# 转换日期格式并提取中文月份
df["日期"] = pd.to_datetime(df["日期"])
df["月份"] = df["日期"].dt.month_name(locale="zh_CN")

# 按区域、月份分组计算平均产量
agg_df = df.groupby(["区域", "月份"])["产量"].mean().reset_index()

绘制条形图

用seaborn快速生成分组条形图:

import seaborn as sns
import matplotlib.pyplot as plt

# 解决中文显示问题
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False

# 绘制图表
plt.figure(figsize=(12, 7))
sns.barplot(data=agg_df, x="区域", y="产量", hue="月份")

# 设置图表标签与标题
plt.title("各区域月度平均产量对比")
plt.xlabel("区域")
plt.ylabel("平均产量")
plt.legend(title="月份")
plt.tight_layout()
plt.show()

注意事项

  • 若数据集日期格式不标准,可在pd.to_datetime()中添加format参数指定格式,比如format="%Y/%m/%d"
  • 如需按月份顺序展示,可先对agg_df按月份数字排序后再绘图
  • 可通过sns.set_theme()更换图表风格,比如sns.set_theme(style="whitegrid")

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 19:36:18