Python 在时间序列图中添加计数列

Python 在时间序列图中添加计数列,python,matplotlib,Python,Matplotlib,我想根据月份和年份绘制平均值 我的数据有两列(计数、平均值),日期作为索引 如图所示,这是一个类似于我的图,其中x是年,y是平均值 这是我的密码 import matplotlib.pyplot as plt diet = df[['mean']] diet.plot(figsize=(20,10), linewidth=5, fontsize=20 ,marker='<') plt.xlabel('Year', fontsize=20); plt

我想根据月份和年份绘制平均值

我的数据有两列(计数、平均值),日期作为索引

如图所示,这是一个类似于我的图,其中x是年,y是平均值

这是我的密码

    import matplotlib.pyplot as plt
    diet = df[['mean']]
    diet.plot(figsize=(20,10), linewidth=5, fontsize=20 ,marker='<')
    plt.xlabel('Year', fontsize=20);
    plt.xlabel('Month/Year')
    plt.ylabel('mean')
导入matplotlib.pyplot作为plt
饮食=df[[平均值]]
图(figsize=(20,10),线宽=5,fontsize=20,marker=)

这能回答你的问题吗?@DizietAsahi不太可能,因为我想添加列而不是一个字。好吧,你必须运行一个循环,为df的每一行添加一个字。对不起,你的示例成功了,但我的数据出现了这个错误
TypeError:float()参数必须是字符串或数字,而不是“元组”
请提供数据框架的一个片段我认为,因为我的数据是按年份和月份分组的,所以我必须取消分组是的,这与我所想的一样,所以我必须重设索引,它工作正常谢谢
idx = pd.date_range(start='1901-01-01', end='1903-12-31', freq='1M')
df = pd.DataFrame({"mean": np.random.random(size=(idx.size,)), "count": np.random.randint(0,10, size=(idx.size,))}, index=idx)

plt.figure()
ax = df['mean'].plot(figsize=(8,4))
for d,row in df.iterrows():
    ax.annotate('{:.0f}'.format(row['count']), xy=(d,row['mean']), ha='center')