Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the AxesImage instance within AxesSubplot

Is there any way to get a pointer to an AxesImage instance within a subplot? For example, in a general case such as:

import matplotlib.pyplot as plt 
import numpy as np

data = np.random.rand(10,10)

fig, ax = plt.subplots(1,1)

ax.imshow(data)

Getting back the AxesImage object via something like ax.get_image. At the moment I do this via:

ls = [type(x) for x in ax.get_children()]
img = ax.get_children()[ls.index(matplotlib.image.AxesImage)]

And was wondering if there is a less explicit way of doing it.

like image 763
ezatterin Avatar asked Nov 23 '25 07:11

ezatterin


1 Answers

As pointed out by DavidG in the comments, there is a method to get a list of Images within an AxesSubpolot like so:

import matplotlib.pyplot as plt 
import numpy as np

data = np.random.rand(10,10)

fig, ax = plt.subplots(1,1)

ax.imshow(data)

img = ax.get_images()[0] # only one image in this axes instance.
like image 131
ezatterin Avatar answered Nov 25 '25 21:11

ezatterin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!