Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What object does matplotlib.imshow() actually return? How to use this object?

I was trying to run the following code at the moment:

imgplot = plt.imshow(dose_data, extent = [0,4,0,6], aspect = 'auto')

On the console, I typed the following input:

In[38]:imgplot
Out[38]: <matplotlib.image.AxesImage at 0x123ebd198>

And the output made me wonder what the object is. After consulting the documentation, it seems like an AxesImage object (AxesImage Documentation). However I was wondering how to use the object stored in imgplot. At the moment I can neither see what it is, nor save it to an image.

P.S. I know how to show an image and how to save an image. I was just wondering how this object could be useful and what it actually is (like, is it an image object? an array? or some other weird type of object?)

like image 402
Jibbah Dan Avatar asked Feb 02 '17 01:02

Jibbah Dan


1 Answers

As you have already found out, the return type of plt.imshow() is a matplotlib.image.AxesImage. The object img you get when calling img = plt.imshow() is an instance of that class.

In general, you do not have to care about this object, since it's bound to an axes and most of what you want to do, like showing a figure or saving it, does not require any control over that AxesImage itself.

Like with any other object, it may be useful for getting access to some of its properties for later use. e.g. img.cmap returns the colormap of the imgage, img.get_extent() provides you with the image extent.

There are two applications coming to my mind, where the AxesImage is frequently used:

  1. Updating the image data: If you want to change the image data without producing a new plot, you may use
    img.set_data(data)
    This may be useful when working with user interactions or with animations.
  2. Creating a colorbar: When creating a colorbar in a plot with several images, it may not be obvious for which image this colorbar should be created, thus plt.colorbar(img, cax=cax) sets the colorbar of the image img to the axes cax.

For further details, you would probably need to refine your question, asking more specifically about some property, application, potential use etc.

like image 118
ImportanceOfBeingErnest Avatar answered Oct 28 '22 01:10

ImportanceOfBeingErnest