4

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

错误:

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
4

2 回答 2

3

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

plt.clf()

数字之间的功能,但也

plt.close()

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

于 2013-05-28T15:44:42.967 回答
2

您可以使用删除fig实例(完成后)

del fig

你可以删除文件os.unlink

os.unlink('images/img'+str(count))
于 2013-05-28T15:27:31.843 回答