Python matplotlib:指定图形大小时未释放内存
我正在使用 matplotlib 生成许多数值模拟结果图。这些图用作视频中的帧,因此我通过重复调用与此类似的函数来生成其中的许多图:
from pylab import *
def plot_density(filename,i,t,psi_Na):
figure(figsize=(8,6))
imshow(abs(psi_Na)**2,origin = 'lower')
savefig(filename + '_%04d.png'%i)
clf()
问题是,每次调用时,python 进程的内存使用量都会增加几兆字节这个功能。例如,如果我用这个循环调用它:
if __name__ == "__main__":
x = linspace(-6e-6,6e-6,128,endpoint=False)
y = linspace(-6e-6,6e-6,128,endpoint=False)
X,Y = meshgrid(x,y)
k = 1000000
omega = 200
times = linspace(0,100e-3,100,endpoint=False)
for i,t in enumerate(times):
psi_Na = sin(k*X-omega*t)
plot_density('wavefunction',i,t,psi_Na)
print i
那么 ram 使用量会随着时间增长到 600MB。然而,如果我注释掉函数定义中的 figure(figsize=(8,6))
行,那么 ram 使用量将稳定在 52MB。 (8,6)
是默认的图形大小,因此两种情况都会生成相同的图像。我想在不耗尽内存的情况下根据我的数值数据制作不同大小的图。我如何强制 python 释放这些内存?
我已经尝试过 gc.collect() 每个循环来强制垃圾回收,并且我已经尝试过 f = gcf() 来获取当前数字,然后 del f
删除它,但无济于事。
我在 64 位 Ubuntu 10.04 上运行 CPython 2.6.5。
I'm using matplotlib to generate many plots of the results of a numerical simulation. The plots are used as frames in a video, and so I'm generating many of them by repeatedly calling a function similar to this one:
from pylab import *
def plot_density(filename,i,t,psi_Na):
figure(figsize=(8,6))
imshow(abs(psi_Na)**2,origin = 'lower')
savefig(filename + '_%04d.png'%i)
clf()
The problem is that the memory usage of the python process grows by a couple of megabytes with every call to this function. For example if I call it with this loop:
if __name__ == "__main__":
x = linspace(-6e-6,6e-6,128,endpoint=False)
y = linspace(-6e-6,6e-6,128,endpoint=False)
X,Y = meshgrid(x,y)
k = 1000000
omega = 200
times = linspace(0,100e-3,100,endpoint=False)
for i,t in enumerate(times):
psi_Na = sin(k*X-omega*t)
plot_density('wavefunction',i,t,psi_Na)
print i
then the ram usage grows with time to 600MB. If however I comment out the line figure(figsize=(8,6))
in the function definition, then the ram usage stays steady at 52MB. (8,6)
is the default figure size and so identical images are produced in both cases. I'd like to make different sized plots from my numerical data without running out of ram. How might I force python to free up this memory?
I've tried gc.collect()
each loop to force garbage collection, and I've tried f = gcf()
to get the current figure and then del f
to delete it, but to no avail.
I'm running CPython 2.6.5 on 64 bit Ubuntu 10.04.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 pylab.figure 的文档字符串:
所以也许可以尝试:
From the docstring for
pylab.figure
:So perhaps try:
关闭一个数字绝对是一个选择,但是,重复多次,这很耗时。我的建议是拥有一个持久的图形对象(通过 静态函数变量,或作为附加函数参数)。如果该对象是
fig
,则该函数将在每个绘图周期之前调用fig.clf()
。这是时间值
Closing a figure is definitely an option, however, repeated many times, this is time consuming. What I suggest is to have a single persistent figure object (via static function variable, or as additional function argument). If that object is
fig
, the function will then callfig.clf()
before each plotting cycle.Here is the timing values