Question

I'm trying to make a figure with multiple subplots using the lag_plot module of pandas (Lag-1 correlation plots). I'm trying this with the standard 'subplots' command of pyplot, but it doesn't seem to like it when I try to invoke a "lag_plot" on the resulting axes. Here's my code:

c1, c2, c3, c4, c5 = sns.color_palette("husl", 5)
ax1 = plt.subplot2grid((3,2), (0,0))
ax2 = plt.subplot2grid((3,2), (0,1))
ax3 = plt.subplot2grid((3,2), (1,0))
ax4 = plt.subplot2grid((3,2), (1,1))
ax5 = plt.subplot2grid((3,2), (2,0))
ax1.lag_plot(d1, color = c1, alpha=0.5)
ax2.lag_plot(d2, color = c2, alpha=0.5)
ax3.lag_plot(d3, color = c3, alpha=0.5)
ax4.lag_plot(d4, color = c4, alpha=0.5)
ax4.lag_plot(d4, color = c5, alpha=0.5)

Here's the error that results:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/iullah/<ipython-input-78-9deb0f435f22> in <module>()
      5 ax4 = plt.subplot2grid((3,2), (1,1))
      6 ax5 = plt.subplot2grid((3,2), (2,0))
----> 7 ax1.lag_plot(d1, color = c1, alpha=0.5)
      8 ax2.lag_plot(d2, color = c2, alpha=0.5)
      9 ax3.lag_plot(d3, color = c3, alpha=0.5)

AttributeError: 'AxesSubplot' object has no attribute 'lag_plot'

The proper subplot array is produced (2 columns and three rows of subplots, with proper axes), but all blank (of course). So, what am I doing wrong? IS there another way to get multiple subplots when using pandas plotting function?

EDIT: This code works:

c1 = sns.color_palette("YlOrRd_r", 8)

plt.subplot(321)
lag_plot(d1, color = c1, alpha=0.5)

plt.subplot(322)
lag_plot(d2, color = c1, alpha=0.5)

plt.subplot(323)
lag_plot(d3, color = c1, alpha=0.5)

plt.subplot(324)
lag_plot(d4, color = c1, alpha=0.5)

plt.subplot(325)
lag_plot(d5, color = c1, alpha=0.5)

plt.show()

Why does that code work, but not the first? I would greatly prefer to do it the first way, because I can do things like have the rows and columns share axis labels (makes a cleaner looking plot) that I can't do with the second set of code.

Was it helpful?

Solution

You should invoke the plotting command on the data and give it an matplotlib axis:

c1, c2, c3, c4, c5 = sns.color_palette("husl", 5)
ax1 = plt.subplot2grid((3,2), (0,0))
ax2 = plt.subplot2grid((3,2), (0,1))
ax3 = plt.subplot2grid((3,2), (1,0))
ax4 = plt.subplot2grid((3,2), (1,1))
ax5 = plt.subplot2grid((3,2), (2,0))
d1.lag_plot(ax=ax1, color = c1, alpha=0.5)
d2.lag_plot(ax=ax2, color = c2, alpha=0.5)
d3.lag_plot(ax=ax3, color = c3, alpha=0.5)
d4.lag_plot(ax=ax4, color = c4, alpha=0.5)

OTHER TIPS

you can provide the axis to the lag_plot function by ax=... argument:

from pandas.tools.plotting import lag_plot

df = pd.DataFrame ( { ch: np.random.randn(100) for ch in 'AB' } )
fig = plt.figure( )
ax = fig.add_axes( [.05, .05, .9, .9] )

lag_plot( df.A, ax=ax)
lag_plot( df.B, ax=ax, color='Red'  )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top