update an existing contour plot with new data

Hi,

I have some troubles updating a contour plot. I reduced my code to a simple example to reproduce the problem:

[code]
from pylab *
import scipy as sp

x=sp.arange(0,2*sp.pi,0.1)
X,Y=sp.meshgrid(x,x)
f1=sp.sin(X)+sp.sin(Y)
f2=sp.cos(X)+sp.cos(Y)

figure()
C=contourf(f1)
show()

C.set_array(f2)
draw()
[\code]

The problem is that C.set_array(f2) does not show any effect, not even after I call draw(). Shouldn't the array f2 be displayed after that? In comparison, the following code using imshow instead of contour works well:

[code]
figure()
I=imshow(f1)
show()
I.set_data(f2)
draw()
[\code]

What do I need to do to update an existing contour plot with new data?

Greetings
Johannes

The set_array() method (I think) only impacts the colormapping
information for contourf, and even then doesn't appear to update.
What you need to do is make a new contour plot and remove the old one,
especially if you need to change the underlying contoured data. This
should be as easy as C.remove(), but for some reason, this doesn't
exist (I'll go add it in a minute). So instead, you need to do the
following:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 2 * np.pi, 0.1)
X,Y = np.meshgrid(x,x)
f1 = np.sin(X) + np.sin(Y)
f2 = np.cos(X) + np.cos(Y)

plt.figure()
C = plt.contourf(f1)
plt.show()
for coll in C.collections:
    plt.gca().collections.remove(coll)
C = plt.contourf(f2)
plt.draw()

Ryan

···

On Fri, Jul 9, 2010 at 6:28 AM, Johannes Röhrs <johannesro@...1527...> wrote:

I have some troubles updating a contour plot. I reduced my code to a simple example to reproduce the problem:

[code]
from pylab *
import scipy as sp

x=sp.arange(0,2*sp.pi,0.1)
X,Y=sp.meshgrid(x,x)
f1=sp.sin(X)+sp.sin(Y)
f2=sp.cos(X)+sp.cos(Y)

figure()
C=contourf(f1)
show()

C.set_array(f2)
draw()
[\code]

What do I need to do to update an existing contour plot with new data?

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Thanks a lot, this solutions seems to serve my purpose. A new method C.remove() would of course be even better.

One could say the problem is solved, but why does there no method exist to update a contour plot as there is for many other plot routines, i.e.
set_xdata/set_ydata for plot
set_data for imshow or
set_UVC for quiver and so on.
set_array should be the corresponding method for contour plots, and if type C.get_array() I actually get the data array that I used to plot the countours!

My purpose of this is to animate the contour plot, and I did read somewhere that updating the plot is much faster/more efficient than deleting and recreating the plot.

···

----- Original Message -----
From: "Ryan May" <rmay31@...287...>
To: "Johannes Röhrs" <johannesro@...1527...>
Cc: matplotlib-users@lists.sourceforge.net
Sent: Friday, 9 July, 2010 5:11:37 PM
Subject: Re: [Matplotlib-users] update an existing contour plot with new data

On Fri, Jul 9, 2010 at 6:28 AM, Johannes Röhrs <johannesro@...1527...> wrote:

I have some troubles updating a contour plot. I reduced my code to a simple example to reproduce the problem:

[code]
from pylab *
import scipy as sp

x=sp.arange(0,2*sp.pi,0.1)
X,Y=sp.meshgrid(x,x)
f1=sp.sin(X)+sp.sin(Y)
f2=sp.cos(X)+sp.cos(Y)

figure()
C=contourf(f1)
show()

C.set_array(f2)
draw()
[\code]

What do I need to do to update an existing contour plot with new data?

The set_array() method (I think) only impacts the colormapping
information for contourf, and even then doesn't appear to update.
What you need to do is make a new contour plot and remove the old one,
especially if you need to change the underlying contoured data. This
should be as easy as C.remove(), but for some reason, this doesn't
exist (I'll go add it in a minute). So instead, you need to do the
following:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 2 * np.pi, 0.1)
X,Y = np.meshgrid(x,x)
f1 = np.sin(X) + np.sin(Y)
f2 = np.cos(X) + np.cos(Y)

plt.figure()
C = plt.contourf(f1)
plt.show()
for coll in C.collections:
    plt.gca().collections.remove(coll)
C = plt.contourf(f2)
plt.draw()

Ryan

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

This is the case when setting up the initial book-keeping is a
significant portion of the time to make the plot. In this case, most
of the work is in generating the contours, so I don't think you'd get
much savings. (Granted, I haven't tried to verify these assumptions.)

Ryan

···

On Fri, Jul 9, 2010 at 10:49 AM, Johannes Röhrs <johannesro@...1527...> wrote:

Thanks a lot, this solutions seems to serve my purpose. A new method C.remove() would of course be even better.

One could say the problem is solved, but why does there no method exist to update a contour plot as there is for many other plot routines, i.e.
set_xdata/set_ydata for plot
set_data for imshow or
set_UVC for quiver and so on.
set_array should be the corresponding method for contour plots, and if type C.get_array() I actually get the data array that I used to plot the countours!

My purpose of this is to animate the contour plot, and I did read somewhere that updating the plot is much faster/more efficient than deleting and recreating the plot.

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma