如何在同一图中绘制由 statsmodels 绘图函数创建的图

How to draw plots created by statsmodels plotting functions in the same figure

我有以下代码

from statsmodels.graphics.factorplots import interaction_plot
import statsmodels.api as sm
import matplotlib.pyplot as plt

# ...

fig1 = interaction_plot(a, b, c, colors=['red', 'blue'], markers=['D', '^'], ms=10)
fig2 = sm.qqplot(model.resid, line='s')
plt.show()

在单独的 window 中分别生成图 1 和图 2。

我怎样才能把这两个数字画在同一张纸上window?

虽然您会找到很多关于在 matplotlib 中创建两个或更多子图的资源,但这里的问题更具体地要求将 statsmodels.graphics.factorplots.interaction_plotstatsmodels.api.qqplot 生成的两个图创建到同一个图中.

这两个函数都有一个参数 ax,您可以向其提供 matplotlib 坐标轴,以便在该坐标轴内生成绘图。

from statsmodels.graphics.factorplots import interaction_plot
import statsmodels.api as sm
import matplotlib.pyplot as plt

# ...

fig, (ax, ax2) = plt.subplots(nrows=2) # create two subplots, one in each row

interaction_plot(a, b, c, colors=['red', 'blue'], markers=['D', '^'], ms=10, ax=ax)
sm.qqplot(model.resid, line='s', ax=ax2)

plt.show()