Closed
Description
Documentation Link
No response
Problem
Making a colorbar histogram is a kinda popular visualization that's not difficult but I figure finnicky enough that it's worth adding an example to the gallery.
Suggested improvement
I've got one here to explain what I mean but I'm making this an issue so that maybe someone who has a better example can have fun with this:
x = np.random.random(100).reshape(10,10)
counts, bins = np.histogram(x)
cmap = plt.colormaps['viridis']
norm = mcolors.BoundaryNorm(bins, cmap.N)
fig, ax = plt.subplots()
im = ax.imshow(x, cmap=cmap, norm=norm)
cbar = plt.colorbar(im)
cax = ax.inset_axes([1.5, 0, .25, 1])
midpoints = bins[:-1] + np.diff(bins)/2
cax.barh(midpoints, counts, height= 1/len(counts), color = cmap(norm(midpoints)))
cax.set_yticks(bins)
cax.margins(0)
cax.spines[:].set_visible(False)
I'm making cax an inset_axes here mostly because a lot of the real use cases for this are maps where it's nice to have a lot of control on where to put the histogram. I figure this example should live in color. Would be even nicer with #26312 and colorizer 'cause then I think the syncing is managed.