> You can in fact remove the fig argument of the plot_density function as
> it is not used. The clf command removes the children of the figure so
> that it has to delete the axes and its children (amongst which is the
> AxesImage) and create new ones when you invoke imshow again.

I simply removed the clf command!
It works now with this code (I just implemented fig.canvas.draw() as you
indicated):

# BOF
import os, tempfile
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
print '\nBackend:\n' + mpl.get_backend() + '\n'

def plot_density(filename,i,t,psi_Na, image):
    if image is None:
        image = plt.imshow(abs(psi_Na)**2,origin = 'lower')
    else:
        image.set_data(abs(psi_Na)**2)
        fig.canvas.draw()
    filename = os.path.join(tempfile.gettempdir(), filename + '_%04d.png'%i)
    plt.savefig(filename)
    return image

if __name__ == "__main__":
    x = np.linspace(-6e-6,6e-6,128,endpoint=False)
    y = np.linspace(-6e-6,6e-6,128,endpoint=False)
    X,Y = np.meshgrid(x,y)
    k = 1000000
    omega = 200
    times = np.linspace(0,100e-3,100,endpoint=False)
    fig = plt.figure(figsize=(8,6))
    image = None
    for i,t in enumerate(times):
        psi_Na = np.sin(k*X-omega*t)
        image = plot_density('wavefunction',i,t,psi_Na, image)
        print i
    del image, fig
    plt.close()
# EOF


Fabrice Silva-2 wrote:
> 
> Le vendredi 17 juin 2011 à 02:38 -0700, Alain Francés a écrit :
>> Hi,
>> 
>> This way the code creates empty images, it needs indeed some update where
>> you inserted the comment. However I'm not sure that the benefit in
>> term of memory and speed will be important.
> 
> I would think it would avoid creating a new instance of AxesImage and
> calling garbage collector to destroy previous one (if no reference is
> kept) or accumulating children of the figure (if reference to previous
> AxesImages is kept by a parent object).
> 
> Update is done by fig.canvas.draw() (to be put where the comment is)
> 
>> PS: I slightly changed the code below since as Jakob noticed: "since you
>> just use one figure object and don't switch it it is not necessary to
>> pass
>> the object to the plot method - it remains active anyway."
> 
> You can in fact remove the fig argument of the plot_density function as
> it is not used. The clf command removes the children of the figure so
> that it has to delete the axes and its children (amongst which is the
> AxesImage) and create new ones when you invoke imshow again.
> 
> -- 
> Fabrice Silva
> 
> 
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Memory-increasing-while-plotting-in-loop-tp31850163p31867966.html
Sent from the matplotlib - users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to