Pyplot 不绘制子图

Pyplot not plotting subplots

我正在使用 pyplot 创建子图并在其轴上绘制所有内容。我正在尝试同时处理两个单独的数字 figcor。我正在做的是

import matplotlib.pyplot as plt
fig=plt.figure(1)
cor=plt.figure(2)
ax=plt.subplot(111)
bx=plt.subplot(211)

ax.plot(dates_obs,obs,label='L1')

handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, loc='upper left', bbox_to_anchor=(1.,1.))
plt.xticks(rotation=45)

box = ax.get_position()
ax.set_position([box.x0*0.5, box.y0*1.2, box.width * 0.75, box.height*1.05])
ax.grid('on')

fig.savefig('test.png', bbox_extra_artists=(lgd,))

然而,情节是空白的。

我试图将 bx 定义为 cor 的子图,将 ax 定义为 fig 的子图,因此 bx=plt.subplot(211),但我猜这不是参数 2 的意思。

如果我从 plt. 开始工作,没有定义 fig(例如,plt.plot(dates_obs,obs)),它可以工作(句柄定义等除外),但我可以' 这样做是因为我必须同时处理两个图形。

我是不是漏掉了一些基本的东西? 非常感谢,

使用您的代码,您创建了 figcor,然后您创建了 axbx。默认情况下,这些将绘制在您创建的最后一个图形 cor 上。然后,您保存 fig(这是空的,因为绘图在 cor 上)。

对轴定义使用以下内容以确保在正确的图中创建轴:

fig=plt.figure(1)
...
ax=fig.add_subplot(111)
bx=fig.add_subplot(211)
...
fig.savefig('test.png', bbox_extra_artists=(lgd,))

您可以为 cor

做类似的事情