Figure.colorbar(mappable=...)
has an argument to accept colorable items, such as AxesImage
, ContourSet
, etc. with this in mind, the usual usage is following:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
ax = plt.subplot()
im = ax.imshow(np.arange(100).reshape((10, 10)))
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
In above example, the mappable im
is returned by imshow()
, which in most cases I think it's nice. My question is how can we return mappable directly from ax
after we implemented ax.imshow()
or ax.pcolormesh()
, which should look like something below.
ax.imshow(np.arange(100).reshape((10, 10)))
def get_mappable(ax):
pass
mappable = get_mappable(ax)
ax.images
is not what I want, as this is only one type of mappables.
I looked back into some source code in plt.colorbar()
, it uses gci()
(here), but I tried and it returns None
, I guess I must miss something important. Pls give me some direction, thank you very much.
Edits:
I found an answer here, but it is very simialr to ax.images
method.