我有一个看起来像这样的数据集:
prod_code month items cost
0 040201060AAAIAI 2016-05-01 5 572.20
1 040201060AAAKAK 2016-05-01 164 14805.19
2 040201060AAALAL 2016-05-01 13465 14486.07
执行 df.dtypes 显示 month 列是 datetime64[ns] 类型。
我现在正在尝试绘制特定产品的每月成本:
df[df.bnf_code=='040201060AAAIAI'][['month', 'cost']].plot()
plt.show()
这可行,但 x 轴不是我期望的时间戳:
如何使用月份和年份标签很好地格式化 x 轴标签?
更新:我也试过这个,得到一个条形图,它在 x 轴上输出时间戳,但格式很长很笨重:
df[df.bnf_code=='040201060AAAIAI'].plot.bar(x='month', y='cost', title='Spending on 040201060AAAIAI')
请您参考如下方法:
如果将日期设置为索引,则应正确标记 x 轴:
df[df.bnf_code=='040201060AAAIAI'][['month', 'cost']].set_index('month').plot()
我只是将 set_index 添加到您的代码中。
