python – how to get the data from an plt.imshow()?

I have the following code

import scipy.misc
import matplotlib.pyplot as plt

a = plt.imshow(scipy.misc.lena())

and what I’m hoping to achieve is get the data on lena by accessing a or it’s children.

The reason is I’ll be accessing the image as plt.gcf() or plt.gca()

Best answer

a should be a matplotlib.image.AxesImage instance, in which case you can use

a.get_array() 

and

a.set_array(data)

The array is stored as a masked array.

Example

There’s an official example available at http://matplotlib.org/examples/animation/dynamic_image.html.

Direct access

You can also use

a._A

to access the array data directly, though I imagine that the getters and setters are the preferred method.