【发布时间】:2021-01-19 01:15:07
【问题描述】:
我想创作很多,至少10000个情节, 所以我用for循环来做,这是我的代码。
for i in range(0, 10000):
fig, ax = plt.subplots(1, 1, num=0)
x = np.linspace(0, 100, 100)
y = np.sin(x)
ax.plot(x, y)
plt.tight_layout()
fig.savefig("test.png")
plt.close(fig=0)
def plot_test():
for i in range(0, 10000):
fig, ax = plt.subplots(1, 1, num=0)
x = np.linspace(0, 100, 100)
y = np.sin(x)
ax.plot(x, y)
plt.tight_layout()
fig.savefig("test.png")
plt.close(fig=0)
无论在函数内部还是外部使用 plt.close(), 内存还在增加, 我检查了 plt.close("all") 和 plt.close(fig) 也没有工作。 希望有人能告诉我如何正确使用 plt.close()。
【问题讨论】:
-
谢谢,但是那个帖子里的方法对我不起作用。
-
-
我尝试了几种方法后发现内存增加可能是由于我在spyder IDE中运行代码,当我在系统终端中运行时,内存没有明显增加,即使我创建每个循环中都有新的
fig。
标签: python matplotlib