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

如何对pandas数据执行groupby聚合min/max并绘制同图分组柱状图

实现min、max聚合结果同图展示的方法

你可以先通过一次分组聚合同时得到max和min的计算结果,再直接调用plot方法即可自动生成并列柱状图,完整实现代码如下:

import matplotlib.pyplot as plt

# 分组同时计算最大值、最小值
agg_result = df.groupby('fecha_inicio')['capacidad_base_firme'].agg(['max', 'min'])

# 直接绘制双系列柱状图
agg_result.plot(kind='bar')

# 配置图表样式
plt.xlabel('Tarifa de Base firme por Zona')
plt.ylabel('Capacidad Base Firme')
plt.legend(['最大值', '最小值'])
# 若x轴日期标签拥挤可添加以下两行优化显示
plt.xticks(rotation=45, ha='right')
plt.tight_layout()

plt.show()

如果你需要堆叠展示两组柱状图,只需在plot方法中添加参数stacked=True即可:
agg_result.plot(kind='bar', stacked=True)


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 03:15:07