首页 > 解决方案 > 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')

我在这里尝试了这种方法,但它在左边给了我一个日期,就是这样。所以我想我是如何形成我的数据框错误的?

使用具有不同 xlabels 的 Pandas 绘制日期时间条形图

标签: pythonpandasdataframematplotlib

解决方案


推荐阅读