似乎在 matplotlib 中创建图形的标准方法不像我在 python 中所期望的那样:默认情况下,在循环中调用 fig = matplotlib.figure()
将继续到所有的数字创建,并最终耗尽内存。
有quite a few处理变通方法但需要显式调用 matplotlib.pyplot.close(fig)
的帖子似乎有点骇人听闻。我想要的是一种使 fig
引用计数的简单方法,因此我不必担心内存泄漏。有没有办法做到这一点?
最佳答案
如果您在不使用 plt.figure
的情况下创建图形,那么它应该按照您的预期进行引用计数。例如(这也是使用非交互式 Agg 后端。)
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
# The pylab figure manager will be bypassed in this instance.
# This means that `fig` will be garbage collected as you'd expect.
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
关于python - 创建一个引用计数的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16334588/