向函数生成的图形添加子图
Adding a subplot to a figure produced by a function
我使用 statsmodels 的 irf 函数来生成图表。然后,我使用 ax1 = fig.add_subplot(211)
向该图添加一个子图。代码如下所示:
irf = results.irf()
fig = irf.plot( plot_stderr= False, impulse = columns[1], response = columns[0])
ax1 = fig.add_subplot(211)
ax1.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 1], color='red')
ax1.tick_params(axis='y', labelcolor='red')
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 0], color='blue')
ax2.tick_params(axis='y', labelcolor='blue')
plt.subplots_adjust(hspace = 10)
这会产生:
问题:如您所见,下图(irf图)被剪掉了。当我添加 plt.subplots_adjust(hspace = 10)
时,我得到:
这有点帮助,但代价是减少了顶部子图的大小,这是不希望的。
问题:如何在没有任何剪报的情况下获得大小相等的两个子图。我希望我可以从 fig, (ax1, ax2) = plt.subplots(2,1)
开始,然后将 ax1 传递给 irf.plot()。然后我可以控制两个轴。但显然没有办法将斧头传递给irf.plot()。它只是创造了自己的形象。如果有办法的话,我什至会满足于分别创建两个图形,然后将它们连接起来并将它们保存为一个图形。
这里的根本问题是 irf.plot
正在创建一个带有单个子图的图形(一行,一列)。然后,您使用 add_subplot(211)
添加第二个子图,它告诉 matplotlib 将其排列在 2 行配置的顶行。但是原来的次要情节没有被改变,所以它的一部分被新的次要情节隐藏了(ax1
)。
据我所知,irf.plot
在创建时似乎没有任何选项可以控制它。
但我们可以做的是在添加新的子图之前使用 GridSpec
after creation (e.g. like in this answer) 修改子图位置。
例如,这样的方法可能有效:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
irf = results.irf()
fig = irf.plot(plot_stderr=False, impulse=columns[1], response=columns[0])
# Change position of irf plot axes
ax_irf = fig.get_axes()[0]
gs = gridspec.GridSpec(nrows=2, ncols=2)
ax_irf.set_position(gs[0, 0].get_position(fig))
ax_irf.set_subplotspec(gs[0, 0]) # this might be superfluous
# Now add second subplot
ax1 = fig.add_subplot(gs[1, 0])
ax1.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 1], color='red')
ax1.tick_params(axis='y', labelcolor='red')
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 0], color='blue')
ax2.tick_params(axis='y', labelcolor='blue')
我使用 statsmodels 的 irf 函数来生成图表。然后,我使用 ax1 = fig.add_subplot(211)
向该图添加一个子图。代码如下所示:
irf = results.irf()
fig = irf.plot( plot_stderr= False, impulse = columns[1], response = columns[0])
ax1 = fig.add_subplot(211)
ax1.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 1], color='red')
ax1.tick_params(axis='y', labelcolor='red')
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 0], color='blue')
ax2.tick_params(axis='y', labelcolor='blue')
plt.subplots_adjust(hspace = 10)
这会产生:
问题:如您所见,下图(irf图)被剪掉了。当我添加 plt.subplots_adjust(hspace = 10)
时,我得到:
这有点帮助,但代价是减少了顶部子图的大小,这是不希望的。
问题:如何在没有任何剪报的情况下获得大小相等的两个子图。我希望我可以从 fig, (ax1, ax2) = plt.subplots(2,1)
开始,然后将 ax1 传递给 irf.plot()。然后我可以控制两个轴。但显然没有办法将斧头传递给irf.plot()。它只是创造了自己的形象。如果有办法的话,我什至会满足于分别创建两个图形,然后将它们连接起来并将它们保存为一个图形。
这里的根本问题是 irf.plot
正在创建一个带有单个子图的图形(一行,一列)。然后,您使用 add_subplot(211)
添加第二个子图,它告诉 matplotlib 将其排列在 2 行配置的顶行。但是原来的次要情节没有被改变,所以它的一部分被新的次要情节隐藏了(ax1
)。
irf.plot
在创建时似乎没有任何选项可以控制它。
但我们可以做的是在添加新的子图之前使用 GridSpec
after creation (e.g. like in this answer) 修改子图位置。
例如,这样的方法可能有效:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
irf = results.irf()
fig = irf.plot(plot_stderr=False, impulse=columns[1], response=columns[0])
# Change position of irf plot axes
ax_irf = fig.get_axes()[0]
gs = gridspec.GridSpec(nrows=2, ncols=2)
ax_irf.set_position(gs[0, 0].get_position(fig))
ax_irf.set_subplotspec(gs[0, 0]) # this might be superfluous
# Now add second subplot
ax1 = fig.add_subplot(gs[1, 0])
ax1.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 1], color='red')
ax1.tick_params(axis='y', labelcolor='red')
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.plot(df[columns].dropna().index, df[columns].dropna().iloc[:, 0], color='blue')
ax2.tick_params(axis='y', labelcolor='blue')