user3306127
user3306127

Reputation: 91

How to get the AxesImages from matplotlib

all, I use the such code to plot the images

import matplotlib.pyplot as plt

plt.imshow(array,cmap='jet')
plt.show()

however, now I want to get the handle (im) of im=plt.imshow(array,cmap='jet') How can I get the handle of im if I ignore the handle in the second step.

Upvotes: 9

Views: 7013

Answers (2)

Princy
Princy

Reputation: 353

You can call the method get_images() on the ax.

Example code:

ax = plt.gca() 
ax.imshow(array) 
ax.get_images() # returns a list of AxesImage objects if any.

Upvotes: 9

Rutger Kassies
Rutger Kassies

Reputation: 64443

You can retrieve all the children present on the axes and filter on the object type.

Like:

ax = plt.gca()
imgs = [obj for obj in ax.get_children() if isinstance(obj, mpl.image.AxesImage)]

If you have only one AxesImage on the axes it returns a list containing one object, for example:

[<matplotlib.image.AxesImage at 0x7be6400>]

Upvotes: 7

Related Questions