python - Panda DataFrame Plot xaxis.set_major_locator 在条形图上将刻度留空
问题描述
我目前有一个看起来像这样的熊猫数据框
items_out items_in
day
2018-04-10 -5 0
2018-04-11 0 2
2018-04-12 -2 3
2018-04-13 0 0
2018-04-14 0 0
2018-04-15 0 0
2018-04-16 0 4
2018-04-17 0 0
2018-04-18 -3 5
2018-04-19 0 0
2018-04-20 0 0
2018-04-21 0 7
2018-04-22 0 0
2018-04-23 0 0
2018-04-24 0 0
我正在尝试创建一个看起来像这样的条形图
问题是日期根本不显示。这是我目前正在尝试的代码以及我尝试过的一些评论内容(我一直在玩各种东西)
df = pd.DataFrame(data=inventory_movement)
df.set_index('day',inplace=True)
plt.rcParams['figure.figsize'] = [18, 10]
plt.rcParams["axes.grid.axis"] = "y"
#plt.rcParams["axes.grid"] = True
fig, ax = plt.subplots()
ax1 = df.plot(kind='bar',y='items_out',ax=ax,color='r',width=.7)
ax2 = df.plot(kind='bar',y='items_in',ax=ax,color='g',width=.7)\
ax1.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax2.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gcf().autofmt_xdate()
#ax.get_xaxis().set_major_locator(mdates.WeekdayLocator())
#ax.get_xaxis().set_major_formatter(IndexFormatter(df.index))
plt.savefig('/tmp/inventory_change_rate.png',bbox_inches='tight')
我在这里尝试了这种方法,但它在左边给了我一个日期,就是这样。所以我想我是如何形成我的数据框错误的?
解决方案
推荐阅读
- excel - 如何在 Microsoft Excel 中水平验证单元格
- jquery - 甜蜜警报成功消息显示不起作用
- forms - 如何在提交时将 csv 重力表单导出到本地路径?
- ios - 颤振:输入“未来”
' 不是 'Widget?' 类型的子类型 错误 - ios - 如何将缺少的架构添加到链接框架?
- reactjs - 如何在输入表单中设置初始获取的数据
- python - 我无法使“其他”功能正常工作
- vue.js - vue ant design components 使用的最佳方式
- node.js - 尝试将颤振应用程序连接到 Web 套接字时出错
- javascript - 如何在包含动画元素的容器上设置过渡持续时间