gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:22:12 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com