在matplotlib中绘制堆积条形图的平均线图

时间:2020-04-29 08:43:01

标签: python pandas dataframe matplotlib

我在python中使用Matplotlib完成了这种堆积的条形图: enter image description here

这是我的工作代码:

df.resample("M")["ComName"].value_counts().unstack().rename(lambda  x: x.strftime("%b %Y")).plot(kind="bar", stacked=True, figsize=(20,20), color=color2)

如何在该图的顶部绘制每个月平均值的线形图

1 个答案:

答案 0 :(得分:1)

应该是这样的:

s = df.resample("M")["ComName"].value_counts().unstack().rename(lambda x: x.strftime("%b %Y"))
ax = s.plot(kind="bar", stacked=True, figsize=(20,20), color=color2)
ax.plot(s.index, s.mean(1), linewidth=2.0)