0

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())

Bar plots

astrojuanlu
  • 6,744
  • 8
  • 45
  • 105
  • Possibly related: https://github.com/pandas-dev/pandas/pull/42845 – astrojuanlu Oct 02 '21 at 07:50
  • 1
    That PR should show your expected behavior. However, I do think it makes more sense to bar-plot on date as categorical, because after all, bar is intended for categorical x-axis. Wouldn't be meaningful to plot hundreds of bars on a numerical/date x-axis, I mean a line/scatter plot is much better for this use case. And you always have matplotlib if you really want bar plot. – Quang Hoang Oct 02 '21 at 08:45

0 Answers0