Reputation: 91
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
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
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