Skip to content

[Doc]: add histogram as colorbar example #30026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
story645 opened this issue May 7, 2025 · 4 comments
Open

[Doc]: add histogram as colorbar example #30026

story645 opened this issue May 7, 2025 · 4 comments

Comments

@story645
Copy link
Member

story645 commented May 7, 2025

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:

Image

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.

@timhoffm
Copy link
Member

timhoffm commented May 8, 2025

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:

I suggest you just do this. It's good as is. What could be better?

One maybe could have another dataset. With a more interesting histogram. Try the first one from here. https://matplotlib.org/devdocs/gallery/images_contours_and_fields/image_demo.html

You could leave out the colorbar because the histogram has the same info on the vertical.

Other than that I believe deferring to someone else will likely result in more effort in telling and reviewing without added benefit.

@story645
Copy link
Member Author

story645 commented May 8, 2025

You could leave out the colorbar because the histogram has the same info on the vertical.

Yeah that's mostly there as verification that histogram == colorbar

@timhoffm
Copy link
Member

timhoffm commented May 8, 2025

You can also leave it to illustate the point. Then, I'd add a comment on that.

@jklymak
Copy link
Member

jklymak commented May 8, 2025

This is a nice idea.

I'd make the bins more purposeful - in the above dataset, bins = np.linspace(0, 1, 11) would be much more useful than the default. Agreed that a better dataset would be more interesting though. Maybe the MRI one would be cool, since it's hard to get a feel for how much of each color there is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants