I'm plotting some time series data, using both line plots and bar plots, and I observe a quirky or annoying behavior on pandas bar plots when sharing the X axis.
- On the left, a pandas line plot + a call to
Axes.bar
directly. This is the expected result. - On the right, a pandas line plot + a pandas bar plot. The x axis of the line plot automatically turns into a float and the line disappears somewhere, probably because the change in range or scale. Also, the x axis of the bar plot displays every single date (this can be corrected with
ax[1, 1].xaxis.set_major_locator(mdates.MonthLocator())
, but the result is not the same).
Is there a way to make pandas bar plots behave like raw matplotlib bar plots in this case?
Code:
# close100 and volume100 are time series with the same date index
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 6))
close100.plot(ax=ax[0, 0])
ax[1, 0].bar(volume100.index, volume100.values) # Using Axes.bar
close100.plot(ax=ax[1, 0])
volume100.plot.bar(ax=ax[1, 1]) # Using Series.plot.bar
ax[1, 1].xaxis.set_major_locator(mdates.MonthLocator())