“RuntimeWarning:已打开20个图形”,即使只创建一个图形

2024-05-16 03:41:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我不断收到以下警告:

RuntimeWarning: 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcP ││  `figure.max_open_warning`).

下面是我的代码中使用matplotlib的部分。请注意,我正在使用flask生成要显示的图像

import matplotlib.pyplot as plt
...
fig, ax = plt.subplots()
plt.imshow(plot_data, extent=[0, max_distance,  y_lims[0], y_lims[1]],cmap=cmap, origin='lower', norm=Normalize(-20, 75),aspect=300)
ax.yaxis_date()
...
ax.yaxis.set_major_formatter(yfmt)
ax.yaxis.set_major_locator( mdates.HourLocator(interval = 1))
plt.grid(b=True, which='major', color='#666666', linestyle='-')

figdata = BytesIO()
fig.tight_layout()
fig.savefig(figdata, format='png')
plt.close(fig)
# Return image
resp = Response(figdata.getvalue(), mimetype='image/png')

代码只运行一次,所以我不明白为什么会出现错误


Tags: the代码matplotlibfigpltaxmaxlims