I am trying to do a simple bar chart for a dataframe, that has dates as index and several columns of data.
start_date = pd.to_datetime('20180101')
dates = pd.date_range(start_date, periods=365)
df = pd.DataFrame(np.random.randn(365,4), index = dates, columns = list('ABCD'))
df = df.resample('M').sum()
If I use
df.plot.bar()
Then dates on the x-axis are messed up. If I use
fig, ax = plt.subplots()
ax.bar(df.index, df['A'],width = 5, align = 'center')
ax.bar(df.index, df['B'], width = 5 , align = 'center')
ax.bar(df.index, df['C'],width = 5, align = 'center')
ax.bar(df.index, df['D'], width = 5 , align = 'center')
ax.xaxis_date()
ax.get_xaxis().set_major_locator(mdates.MonthLocator())
ax.get_xaxis().set_major_formatter(mdates.DateFormatter("%b %Y"))
fig.autofmt_xdate()
plt.show()
Then my bars overlap each other and dates are shown with a shift by one month.
Can someone please suggest an elegant solution how to plot a bar chart that will not have the above mentioned drawbacks?