Python 使用pyplot子图和lag_图

Python 使用pyplot子图和lag_图,python,matplotlib,pandas,subplot,Python,Matplotlib,Pandas,Subplot,我正在尝试使用pandas的lag_图模块(lag-1相关图)绘制一个包含多个子图的图。我正在使用pyplot的标准'subplot'命令来尝试这一点,但是当我尝试在结果轴上调用“lag_plot”时,它似乎不喜欢它。这是我的密码: 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

我正在尝试使用pandas的lag_图模块(lag-1相关图)绘制一个包含多个子图的图。我正在使用pyplot的标准'subplot'命令来尝试这一点,但是当我尝试在结果轴上调用“lag_plot”时,它似乎不喜欢它。这是我的密码:

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)
以下是导致的错误:

---------------------------------------------------------------------------
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'

为什么那个代码可以工作,但第一个却不行?我更喜欢用第一种方法,因为我可以做一些事情,比如让行和列共享轴标签(使绘图看起来更清晰),这是我用第二组代码做不到的

您可以通过
ax=…
参数为
lag\u plot
函数提供轴:

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

应在数据上调用plotting命令,并为其指定matplotlib轴:

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)

谢谢你的帮助。虽然我确信这种方式同样好,但另一个答案对我来说似乎更直观一些。。。
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)