【问题标题】:Memory full Python plotting 3d内存满 Python 绘图 3d
【发布时间】:2013-05-23 15:09:03
【问题描述】:

我在循环绘制图像时遇到了一些内存问题。如何删除旧的?

错误:

Traceback (most recent call last):
  File "C:\Users\Alex\Dropbox\code stuff\solarsystem.py", line 69, in <module>
    fig = plt.figure()
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 343, in figure
    **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 106, in new_figure_manager_given_figure
    canvas = FigureCanvasTkAgg(figure, master=window)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 225, in __init__
    master=self._tkcanvas, width=w, height=h)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 3306, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 3262, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: not enough free memory for image buffer

脚本:

count = 0
xy = bodies[0][2]
x = [[a[0] for a in bodies[0][2]]]
y = [[a[1] for a in bodies[0][2]]]
z = [[a[2] for a in bodies[0][2]]]

for i in range(1,nbodies):
    x.append([a[0] for a in bodies[i][2]])
    y.append([a[1] for a in bodies[i][2]])
    z.append([a[2] for a in bodies[i][2]])

for j in range(0,len(bodies[0][2])-1,10):
    Xc = [[x[0][j]]]
    Yc = [[y[0][j]]]
    Zc = [[z[0][j]]]
    for k in range(1,nbodies):
        Xc.append([x[k][j]])
        Yc.append([y[k][j]])
        Zc.append([z[k][j]])

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    for l in range(len(Xc)):
        ax.scatter( Xc[l], Yc[l], Zc[l], c=(i/nbodies,i/nbodies,i/nbodies))

    ax.axis([-400, 400, -400, 400])
    ax.set_zlim(-400, 400)

    pylab.savefig('images/img'+str(count))
    pylab.clf()
    count += 1

    percent = (j/(len(bodies[0][2])-1.))*100
    if percent % 10 ==0:
        print percent

【问题讨论】:

  • 是这个,还是你生成了一些这样的数字?
  • 大约10k atm,想赚更多:P
  • 酷,看看我的回答,希望对你有帮助

标签: python memory printing matplotlib


【解决方案1】:

如果你要生成多张图片,你不仅需要调用

plt.clf()

数字之间的作用,也是

plt.close()

图片之间的函数(即after the for j in range(0,len(bodies[0][2])-1,10): 循环完成)

【讨论】:

    【解决方案2】:

    您可以使用

    删除fig 实例(完成后)
    del fig
    

    你可以用os.unlink删除文件

    os.unlink('images/img'+str(count))
    

    【讨论】:

    • 我需要保留图像直到循环结束。知道如何在此之后摆脱它们吗?
    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多