Using plt.pcolormesh with FuncAnimation

Hi,

I have plt.pcolormesh plot i would like to animate. So i've taken a
look at the various examples and decided to go with the FuncAnimation
routine. This works for me, but im using for every frame a new call to
plt.colormesh and i am not updating the underlaying data, like in this
example
http://matplotlib.sourceforge.net/examples/animation/dynamic_image.html
This is because there seems to be no set_data, set_array or similar
for the from plt.colormesh returned object (an instance of
matplotlib.collection.QuadMesh). Am i right? Is there any way i can
update the data structures of plt.colormesh?

I know i could use a ArtistAnimation like in
http://matplotlib.sourceforge.net/examples/animation/dynamic_image2.html
but i don't want to cache so many images.

Thanks!
Manuel Jung

Hi,

I have plt.pcolormesh plot i would like to animate. So i’ve taken a

look at the various examples and decided to go with the FuncAnimation

routine. This works for me, but im using for every frame a new call to

plt.colormesh and i am not updating the underlaying data, like in this

example

http://matplotlib.sourceforge.net/examples/animation/dynamic_image.html

This is because there seems to be no set_data, set_array or similar

for the from plt.colormesh returned object (an instance of

matplotlib.collection.QuadMesh). Am i right? Is there any way i can

update the data structures of plt.colormesh?

Hi Manuel,

You can call QuadMesh’s set_array method, just as you can for images. (You suggest there is no set_array method for QuadMesh; are you sure about that?) The strange part is that it expects a 1d array, where as colormesh accepts arrays of various dimensions.

import numpy as np
import matplotlib.pyplot as plt
mesh = plt.pcolormesh(np.random.rand(10,10))
mesh.set_array(np.random.rand(10,10).ravel())

plt.draw()

set_array doesn’t complain if you remove the call to ravel, but plt.draw() will complain.

-Tony

···

On Sun, Dec 4, 2011 at 9:59 AM, Manuel Jung <mjung@…3875…> wrote:

Hi,

You are right. I have tried set_array before, but forgot to mention
it. I have retried now and got it working. Like you said one has to
ravel() the array and use this with QuadMeshs set_array function.

Thanks.
Manuel

2011/12/4 Tony Yu <tsyu80@...287...>:

···

On Sun, Dec 4, 2011 at 9:59 AM, Manuel Jung <mjung@...3874...> > wrote:

Hi,

I have plt.pcolormesh plot i would like to animate. So i've taken a
look at the various examples and decided to go with the FuncAnimation
routine. This works for me, but im using for every frame a new call to
plt.colormesh and i am not updating the underlaying data, like in this
example
http://matplotlib.sourceforge.net/examples/animation/dynamic_image.html
This is because there seems to be no set_data, set_array or similar
for the from plt.colormesh returned object (an instance of
matplotlib.collection.QuadMesh). Am i right? Is there any way i can
update the data structures of plt.colormesh?

Hi Manuel,

You can call QuadMesh's `set_array` method, just as you can for images. (You
suggest there is no set_array method for QuadMesh; are you sure about that?)
The strange part is that it expects a 1d array, where as colormesh accepts
arrays of various dimensions.

import numpy as np
import matplotlib.pyplot as plt
mesh = plt.pcolormesh(np.random.rand(10,10))
mesh.set_array(np.random.rand(10,10).ravel())
plt.draw()

set_array doesn't complain if you remove the call to `ravel`, but
`plt.draw()` will complain.

-Tony