python - 创建一个引用计数的图形

标签 python memory-management matplotlib

似乎在 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/

相关文章:

python - pysftp,paramiko,请求 : Error reading SSH protocol banner

python - 使用 ECMAScript 正则表达式验证 Python 列表索引语法

python - numpy 中的通用函数概念和符号

.net - .NET (wpf) 应用程序的内存管理

Python 从线程更新 Matplotlib

Python 设置.py : How to get find_packages() to identify packages in subdirectories

c - 当只需要少量时,使用 unsigned char 类型是否可以接受?

c++ - 异常安全构造函数

python - %matplotlib 内联魔法导入错误

python - Python 3.5.2 中的本福德定律绘图