Evgeny
Evgeny

Reputation: 11

matplotlib bar chart with multiple columns, dates on x axis

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?

Upvotes: 1

Views: 3079

Answers (1)

Marcus Grass
Marcus Grass

Reputation: 1083

I did a similar project with bars and used this example to get the formatting correct.

Since i don't have the mdates values I can't run it to check if it's correct in your case but try doing this:

...
fig, ax = plt.subplots()
width = 5
ax.bar(df.index, df['A'], width, align = 'center')
ax.bar(df.index + width , df['B'], width, align = 'center')
ax.bar(df.index + 2*width, df['C'], width, align = 'center')
ax.bar(df.index + 3*width, df['D'], width, align = 'center')
ax.set_xticks(df.index + width) # sets the x-ticks to the middle of the cluster of bars
ax.xaxis_date()
...

Upvotes: 3

Related Questions