Memory increasing while plotting in loop

Hi,

I have a script that creates and saves figures in a loop. The memory is increasing at each figure and is not released back, rising a Memory error.
I used the close() function on the figure object as well as gc.collect(), but no effect.

I searched on the net and found a similar problem at http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size. The problem here was solved using the close() function but in my case, as refered before, it doens't work (see code below).

I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on windows7.

Thanks for help!

Alain

CODE:

import pylab as pl
import os, tempfile
def plot_density(filename,i,t,psi_Na):
    pl.figure(figsize=(8,6))
    pl.imshow(abs(psi_Na)**2,origin = 'lower')
    filename = os.path.join(tempfile.gettempdir(), filename + '_%04d.png'%i)
    pl.savefig(filename)
    pl.clf()
    pl.close()
if __name__ == "__main__":
    x = pl.linspace(-6e-6,6e-6,128,endpoint=False)
    y = pl.linspace(-6e-6,6e-6,128,endpoint=False)
    X,Y = pl.meshgrid(x,y)
    k = 1000000
    omega = 200
    times = pl.linspace(0,100e-3,100,endpoint=False)
    for i,t in enumerate(times):
        import pylab as pl
        psi_Na = pl.sin(k*X-omega*t)
        plot_density('wavefunction',i,t,psi_Na)
        print i

You are importing pylab again before each plot. You have loaded it before.

for i,t in enumerate(times):
       import pylab as pl # <--ERASE THIS

···

On Wed, Jun 15, 2011 at 12:10 PM, Alain Pascal Frances <frances17404@...3388...> wrote:

Hi,

I have a script that creates and saves figures in a loop. The memory is increasing at each figure and is not released back, rising a Memory error.
I used the close() function on the figure object as well as gc.collect(), but no effect.

I searched on the net and found a similar problem at http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size. The problem here was solved using the close() function but in my case, as refered before, it doens't work (see code below).

I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on windows7.

Thanks for help!

Alain

CODE:

import pylab as pl
import os, tempfile
def plot_density(filename,i,t,psi_Na):
pl.figure(figsize=(8,6))
pl.imshow(abs(psi_Na)**2,origin = 'lower')
filename = os.path.join(tempfile.gettempdir(), filename + '_%04d.png'%i)
pl.savefig(filename)
pl.clf()
pl.close()
if __name__ == "__main__":
x = pl.linspace(-6e-6,6e-6,128,endpoint=False)
y = pl.linspace(-6e-6,6e-6,128,endpoint=False)
X,Y = pl.meshgrid(x,y)
k = 1000000
omega = 200
times = pl.linspace(0,100e-3,100,endpoint=False)
for i,t in enumerate(times):
import pylab as pl
psi_Na = pl.sin(k*X-omega*t)
plot_density('wavefunction',i,t,psi_Na)
print i
------------------------------------------------------------------------------
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

Hi, http://old.nabble.com/file/p31858795/ErrorMsg.png

indeed I forgot to remove these line from previous tests, sorry.

However it doesn't affect the code behaviour, the problem remains (it fills
the RAM memory and raises a memory error, see img in att.).

Alain

Davidmh wrote:

···

You are importing pylab again before each plot. You have loaded it before.

for i,t in enumerate(times):
       import pylab as pl # <--ERASE THIS

On Wed, Jun 15, 2011 at 12:10 PM, Alain Pascal Frances > <frances17404@...3388...> wrote:

Hi,

I have a script that creates and saves figures in a loop. The memory is
increasing at each figure and is not released back, rising a Memory
error.
I used the close() function on the figure object as well as gc.collect(),
but no effect.

I searched on the net and found a similar problem at
http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size.
The problem here was solved using the close() function but in my case, as
refered before, it doens't work (see code below).

I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on windows7.

Thanks for help!

Alain

CODE:

import pylab as pl
import os, tempfile
def plot_density(filename,i,t,psi_Na):
pl.figure(figsize=(8,6))
pl.imshow(abs(psi_Na)**2,origin = 'lower')
filename = os.path.join(tempfile.gettempdir(), filename +
'_%04d.png'%i)
pl.savefig(filename)
pl.clf()
pl.close()
if __name__ == "__main__":
x = pl.linspace(-6e-6,6e-6,128,endpoint=False)
y = pl.linspace(-6e-6,6e-6,128,endpoint=False)
X,Y = pl.meshgrid(x,y)
k = 1000000
omega = 200
times = pl.linspace(0,100e-3,100,endpoint=False)
for i,t in enumerate(times):
import pylab as pl
psi_Na = pl.sin(k*X-omega*t)
plot_density('wavefunction',i,t,psi_Na)
print i
------------------------------------------------------------------------------
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

------------------------------------------------------------------------------
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-tp31850163p31858795.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

I'm not sure if it helps, but you can try to plot always into the same figure object instead of creating a new one every time you call the plot_density function.

I would create one figure in advance and pass this the plot_density method. Within the method set the figure active (pl.figure(fig.number)), plot the data, save the figure and clear the figure canvas (pl.clf() should work here, I guess).
Well, as mentioned I'm not sure but you could give it a try.

br
Jakob

Alain Francés wrote:

···

Hi, http://old.nabble.com/file/p31858795/ErrorMsg.png

indeed I forgot to remove these line from previous tests, sorry.

However it doesn't affect the code behaviour, the problem remains (it fills
the RAM memory and raises a memory error, see img in att.).

Alain

Davidmh wrote:

You are importing pylab again before each plot. You have loaded it before.

for i,t in enumerate(times):
       import pylab as pl # <--ERASE THIS

On Wed, Jun 15, 2011 at 12:10 PM, Alain Pascal Frances >> <frances17404@...3388...> wrote:

Hi,

I have a script that creates and saves figures in a loop. The memory is
increasing at each figure and is not released back, rising a Memory
error.
I used the close() function on the figure object as well as gc.collect(),
but no effect.

I searched on the net and found a similar problem at
http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size.
The problem here was solved using the close() function but in my case, as
refered before, it doens't work (see code below).

I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on windows7.

Thanks for help!

Alain

CODE:

import pylab as pl
import os, tempfile
def plot_density(filename,i,t,psi_Na):
   pl.figure(figsize=(8,6))
   pl.imshow(abs(psi_Na)**2,origin = 'lower')
   filename = os.path.join(tempfile.gettempdir(), filename +
'_%04d.png'%i)
   pl.savefig(filename)
   pl.clf()
   pl.close()
if __name__ == "__main__":
   x = pl.linspace(-6e-6,6e-6,128,endpoint=False)
   y = pl.linspace(-6e-6,6e-6,128,endpoint=False)
   X,Y = pl.meshgrid(x,y)
   k = 1000000
   omega = 200
   times = pl.linspace(0,100e-3,100,endpoint=False)
   for i,t in enumerate(times):
       import pylab as pl
       psi_Na = pl.sin(k*X-omega*t)
       plot_density('wavefunction',i,t,psi_Na)
       print i
------------------------------------------------------------------------------

I can run the script you provided up to 600 without noticing any
memory growth (TkAgg backend by default). Using the WxAgg, the memory
leak appears.

If you don't explicitly need WxAgg, I would recommend using TkAgg, or
better Agg if you are not showing the figures. If it is not the case,
more expert and wiser people will have to come to help.

···

On Thu, Jun 16, 2011 at 11:41 AM, Alain Francés <frances08512@...3388...> wrote:

Hi, http://old.nabble.com/file/p31858795/ErrorMsg.png

indeed I forgot to remove these line from previous tests, sorry.

However it doesn't affect the code behaviour, the problem remains (it fills
the RAM memory and raises a memory error, see img in att.).

Alain

Davidmh wrote:

You are importing pylab again before each plot. You have loaded it before.

for i,t in enumerate(times):
import pylab as pl # <--ERASE THIS

On Wed, Jun 15, 2011 at 12:10 PM, Alain Pascal Frances >> <frances17404@...3388...> wrote:

Hi,

I have a script that creates and saves figures in a loop. The memory is
increasing at each figure and is not released back, rising a Memory
error.
I used the close() function on the figure object as well as gc.collect(),
but no effect.

I searched on the net and found a similar problem at
http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size.
The problem here was solved using the close() function but in my case, as
refered before, it doens't work (see code below).

I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on windows7.

Thanks for help!

Alain

CODE:

import pylab as pl
import os, tempfile
def plot_density(filename,i,t,psi_Na):
pl.figure(figsize=(8,6))
pl.imshow(abs(psi_Na)**2,origin = 'lower')
filename = os.path.join(tempfile.gettempdir(), filename +
'_%04d.png'%i)
pl.savefig(filename)
pl.clf()
pl.close()
if __name__ == "__main__":
x = pl.linspace(-6e-6,6e-6,128,endpoint=False)
y = pl.linspace(-6e-6,6e-6,128,endpoint=False)
X,Y = pl.meshgrid(x,y)
k = 1000000
omega = 200
times = pl.linspace(0,100e-3,100,endpoint=False)
for i,t in enumerate(times):
import pylab as pl
psi_Na = pl.sin(k*X-omega*t)
plot_density('wavefunction',i,t,psi_Na)
print i
------------------------------------------------------------------------------
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

------------------------------------------------------------------------------
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-tp31850163p31858795.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

Hi,

I'm using WXAgg by default because I developped an application with GUI
using wxpython.
But in this case indeed I don't need WXAgg and substituting WXAgg by Agg
just work fine, both in the code I submitted as example (see the corrected
code at the end) and in the one I'm working with.
So yes, probably there is a memory leakage using WXAgg that, as you say, can
be an interesting problem for the experts!

Thank you very much for your wise solution (if you are interested, see also
the comment of Jakob and my reply, he proposed an elegant solution still
using WXAgg).

Alain

CORRECTED CODE:
# BOF
import os, tempfile
import matplotlib as mpl
if mpl.get_backend<>'agg':
    mpl.use('agg')
import matplotlib.pyplot as plt
import numpy as np
print '\nBackend:\n' + mpl.get_backend() + '\n'

def plot_density(filename,i,t,psi_Na):
    plt.figure(figsize=(8,6))
    plt.imshow(abs(psi_Na)**2,origin = 'lower')
    filename = os.path.join(tempfile.gettempdir(), filename + '_%04d.png'%i)
    plt.savefig(filename)
    plt.close()

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,500,endpoint=False)
    for i,t in enumerate(times):
        psi_Na = np.sin(k*X-omega*t)
        plot_density('wavefunction',i,t,psi_Na)
        print i
# EOF

Davidmh wrote:

···

I can run the script you provided up to 600 without noticing any
memory growth (TkAgg backend by default). Using the WxAgg, the memory
leak appears.

If you don't explicitly need WxAgg, I would recommend using TkAgg, or
better Agg if you are not showing the figures. If it is not the case,
more expert and wiser people will have to come to help.

On Thu, Jun 16, 2011 at 11:41 AM, Alain Francés <frances08512@...3634...8...> > wrote:

Hi, http://old.nabble.com/file/p31858795/ErrorMsg.png

indeed I forgot to remove these line from previous tests, sorry.

However it doesn't affect the code behaviour, the problem remains (it
fills
the RAM memory and raises a memory error, see img in att.).

Alain

Davidmh wrote:

You are importing pylab again before each plot. You have loaded it
before.

for i,t in enumerate(times):
import pylab as pl # <--ERASE THIS

On Wed, Jun 15, 2011 at 12:10 PM, Alain Pascal Frances >>> <frances17404@...3388...> wrote:

Hi,

I have a script that creates and saves figures in a loop. The memory is
increasing at each figure and is not released back, rising a Memory
error.
I used the close() function on the figure object as well as
gc.collect(),
but no effect.

I searched on the net and found a similar problem at
http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size.
The problem here was solved using the close() function but in my case,
as
refered before, it doens't work (see code below).

I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on
windows7.

Thanks for help!

Alain

CODE:

import pylab as pl
import os, tempfile
def plot_density(filename,i,t,psi_Na):
pl.figure(figsize=(8,6))
pl.imshow(abs(psi_Na)**2,origin = 'lower')
filename = os.path.join(tempfile.gettempdir(), filename +
'_%04d.png'%i)
pl.savefig(filename)
pl.clf()
pl.close()
if __name__ == "__main__":
x = pl.linspace(-6e-6,6e-6,128,endpoint=False)
y = pl.linspace(-6e-6,6e-6,128,endpoint=False)
X,Y = pl.meshgrid(x,y)
k = 1000000
omega = 200
times = pl.linspace(0,100e-3,100,endpoint=False)
for i,t in enumerate(times):
import pylab as pl
psi_Na = pl.sin(k*X-omega*t)
plot_density('wavefunction',i,t,psi_Na)
print i
------------------------------------------------------------------------------
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

------------------------------------------------------------------------------
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-tp31850163p31858795.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

------------------------------------------------------------------------------
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-tp31850163p31861233.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi Jakob,

yes your solution works and is more elegant! See the code at the end of this
message.
However using the original code with the Agg backend instead of WXAgg also
works fine, so there is probably a memory leakage problem with WXAgg (see
the post of David and my reply).
Thank for your reply and suggestion!

Alain

CODE
# 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, fig):
    plt.imshow(abs(psi_Na)**2,origin = 'lower')
    filename = os.path.join(tempfile.gettempdir(), filename + '_%04d.png'%i)
    plt.savefig(filename)
    plt.clf()

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))
    for i,t in enumerate(times):
        psi_Na = np.sin(k*X-omega*t)
        plot_density('wavefunction',i,t,psi_Na, fig)
        print i
    plt.close()
# EOF

Jakob Gager wrote:

···

Hi,

I'm not sure if it helps, but you can try to plot always into the same
figure object instead of creating a new one every time you call the
plot_density function.

I would create one figure in advance and pass this the plot_density
method. Within the method set the figure active (pl.figure(fig.number)),
plot the data, save the figure and clear the figure canvas (pl.clf()
should work here, I guess).
Well, as mentioned I'm not sure but you could give it a try.

br
Jakob

Alain Francés wrote:

Hi, http://old.nabble.com/file/p31858795/ErrorMsg.png

indeed I forgot to remove these line from previous tests, sorry.

However it doesn't affect the code behaviour, the problem remains (it
fills
the RAM memory and raises a memory error, see img in att.).

Alain

Davidmh wrote:

You are importing pylab again before each plot. You have loaded it
before.

for i,t in enumerate(times):
       import pylab as pl # <--ERASE THIS

On Wed, Jun 15, 2011 at 12:10 PM, Alain Pascal Frances >>> <frances17404@...3388...> wrote:

Hi,

I have a script that creates and saves figures in a loop. The memory is
increasing at each figure and is not released back, rising a Memory
error.
I used the close() function on the figure object as well as
gc.collect(),
but no effect.

I searched on the net and found a similar problem at
http://stackoverflow.com/questions/3623600/python-matplotlib-memory-not-being-released-when-specifying-figure-size.
The problem here was solved using the close() function but in my case,
as
refered before, it doens't work (see code below).

I'm using Python 2.6.6, matplotlib 1.0.1, WXagg as backend, on
windows7.

Thanks for help!

Alain

CODE:

import pylab as pl
import os, tempfile
def plot_density(filename,i,t,psi_Na):
   pl.figure(figsize=(8,6))
   pl.imshow(abs(psi_Na)**2,origin = 'lower')
   filename = os.path.join(tempfile.gettempdir(), filename +
'_%04d.png'%i)
   pl.savefig(filename)
   pl.clf()
   pl.close()
if __name__ == "__main__":
   x = pl.linspace(-6e-6,6e-6,128,endpoint=False)
   y = pl.linspace(-6e-6,6e-6,128,endpoint=False)
   X,Y = pl.meshgrid(x,y)
   k = 1000000
   omega = 200
   times = pl.linspace(0,100e-3,100,endpoint=False)
   for i,t in enumerate(times):
       import pylab as pl
       psi_Na = pl.sin(k*X-omega*t)
       plot_density('wavefunction',i,t,psi_Na)
       print i
------------------------------------------------------------------------------

------------------------------------------------------------------------------
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-tp31850163p31861342.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Have you tried to go further and reuse the AxesImage instance created by
imshow? The API is quite not accessible on official matplotlib
documentation but exists (in the matplotlib.image module)

CODE
# 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, fig, image):
    if image is None:
        image = plt.imshow(abs(psi_Na)**2,origin = 'lower')
    else:
        image.set_data(abs(psi_Na)**2)
        # Maybe needs some update here.
    filename = os.path.join(tempfile.gettempdir(), filename + '_%04d.png'%i)
    plt.savefig(filename)
    plt.clf()
    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, fig, image)
        print i
    plt.close()
# EOF

···

Le jeudi 16 juin 2011 à 07:47 -0700, Alain Francés a écrit :

Hi Jakob,

yes your solution works and is more elegant! See the code at the end of this
message.
However using the original code with the Agg backend instead of WXAgg also
works fine, so there is probably a memory leakage problem with WXAgg (see
the post of David and my reply).
Thank for your reply and suggestion!

--
Fabrice Silva

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.
A.

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."

Fabrice Silva-2 wrote:

···

Le jeudi 16 juin 2011 à 07:47 -0700, Alain Francés a écrit :

Hi Jakob,

yes your solution works and is more elegant! See the code at the end of
this
message.
However using the original code with the Agg backend instead of WXAgg
also
works fine, so there is probably a memory leakage problem with WXAgg (see
the post of David and my reply).
Thank for your reply and suggestion!

Have you tried to go further and reuse the AxesImage instance created by
imshow? The API is quite not accessible on official matplotlib
documentation but exists (in the matplotlib.image module)

CODE
# 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)
        # Maybe needs some update here.
    filename = os.path.join(tempfile.gettempdir(), filename +
'_%04d.png'%i)
    plt.savefig(filename)
    plt.clf()
    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
    plt.close()
# EOF
--
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-tp31850163p31867626.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

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.

···

Le vendredi 17 juin 2011 à 02:38 -0700, Alain Francés a écrit :

--
Fabrice Silva

In fact it creates empty images because of the clf command! Image data
is modified but it no more related to the figure...

···

Le vendredi 17 juin 2011 à 12:00 +0200, Fabrice Silva a écrit :

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.

--
Fabrice Silva

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.

So? what about timings and memory consumption?

···

Le vendredi 17 juin 2011 à 03:49 -0700, Alain Francés a écrit :

> 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)

--
Fabrice Silva

By monitoring memory and time with the task manager it looks like there is no
big difference. I'm aware that it should be monitored in a more rigourous
way but I don't think it is really relevant since the main problem was
solved using the Agg backend instead of the WXAgg that seems to have a
memory leakage (see branch discussion with David).

Fabrice Silva-2 wrote:

···

Le vendredi 17 juin 2011 à 03:49 -0700, Alain Francés a écrit :

> 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)

So? what about timings and memory consumption?
--
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-tp31850163p31870257.html
Sent from the matplotlib - users mailing list archive at Nabble.com.