我正在循环绘制并保存数千个文件以供以后的动画使用,如下所示:
import matplotlib.pyplot as plt
for result in results:
plt.figure()
plt.plot(result) # this changes
plt.xlabel('xlabel') # this doesn't change
plt.ylabel('ylabel') # this doesn't change
plt.title('title') # this changes
plt.ylim([0,1]) # this doesn't change
plt.grid(True) # this doesn't change
plt.savefig(location, bbox_inches=0) # this changes
Run Code Online (Sandbox Code Playgroud)
当我运行它并获得大量结果时,它会在保存数千个图后崩溃。我想我想做的是重用我的轴,就像这个答案一样: https: //stackoverflow.com/a/11688881/354979,但我不明白如何。我该如何优化它?
我会创建一个图形并每次清除该图形(使用.clf
)。
import matplotlib.pyplot as plt
fig = plt.figure()
for result in results:
fig.clf() # Clears the current figure
...
Run Code Online (Sandbox Code Playgroud)
plt.figure
由于每次调用都会创建一个新的图形对象,因此内存不足。根据 @tcaswell 的评论,我认为这会比.close
. 差异的解释如下:
何时使用 cla()、clf() 或 close() 清除 matplotlib 中的绘图?
归档时间: |
|
查看次数: |
3183 次 |
最近记录: |