pythonpandasmatplotlibplotautocorrelation

How to add Pandas autocorrelation plot as a subplot?


I have 4 subplots...

fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(nrows=6, ncols=1, figsize=(12, 14))

ax1.plot(df.index, df['a'], linewidth=0.5, color='w')
ax2.plot(df.index, df['b'], linewidth=0.5, color='w')
ax3.plot(df.index, df['c'], linewidth=0.75, color='r')
ax4.plot(df.index, df['d'], linewidth=0.75, color='g')

and want to a 5th, which happens to be a Pandas autocorrelation...

x = pd.plotting.autocorrelation_plot(df['a'])
ax5.subplot(x)

Unfortunately this just shows the last plot obliterating the other previous 4.

Do you have any clues on how to show all 5 plots?


Solution

  • As commented, autocorrelation_plot (and pandas plotting functions in general) accept an ax param:

    pd.plotting.autocorrelation_plot(df['a'], ax=ax5)