gpt4 book ai didi

python - 内存已满 Python 绘图 3d

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:06 26 4
gpt4 key购买 nike

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

错误:

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

最佳答案

如果您要生成大量图片,您不仅需要调用

plt.clf()

数字之间的函数,也是

plt.close()

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

关于python - 内存已满 Python 绘图 3d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16795556/

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