Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add example to histogram colorbar on galleries#30107

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
livlutz wants to merge21 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromlivlutz:histogram-colorgram

Conversation

livlutz
Copy link

PR summary

This PR contains an example code for generating histogram colorbar on the galleries section with a reference to it on the readme file
closes#30026

PR checklist

@github-actionsgithub-actionsbot added the Documentation: examplesfiles in galleries/examples labelMay 25, 2025
@livlutz
Copy link
Author

@story645 thanks for the suggestion! This PR is ready for review

@rcomer
Copy link
Member

It looks like something went wrong with the layout. Also, please read the comments on the issue for ideas how to improve this.

livlutz reacted with thumbs up emoji

@livlutz
Copy link
Author

livlutz commentedMay 25, 2025
edited
Loading

thanks for the feedback, I tried to make some adjustments but I cannot see if my changes were successful, how does this link you commented before work?

@rcomer
Copy link
Member

rcomer commentedMay 25, 2025
edited
Loading

how does this link you commented before work?

At the bottom of this page are some automated checks. Scroll to the bottom of them and you see "View the built docs". Click on that and you can then navigate to your example from there.

But you can also just run your script locally to see how the plot looks. That will give you faster feedback.

Copy link
Member

@story645story645 left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

As@rcomer said, please look at the original issue - particularly the suggestion to use binomially distributed data .

@@ -5,3 +5,5 @@ Color

For a description of the colormaps available in Matplotlib,
see the :ref:`colormaps tutorial <tutorials-colors>`.

- colorbar_histogram.py: Example of a colorbar with a histogram inset.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
- colorbar_histogram.py: Example of a colorbar with a histogram inset.

You don't need to add anything to the readme

@@ -0,0 +1,46 @@
"""
=========================
Colorbar with Histogram
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
ColorbarwithHistogram
HistogramasColorbar

This example is about using a histogram in place of a regular colorbar

Comment on lines 32 to 34
# Create an axes on the right side of ax. The width of cax will be 20% 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="20%", pad=0.05)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

As I said in the original issue, I want this example to use axes_inset for the colorbar b/c I think that is more reflective of the usual use case for these types of plots.


# Plot histogram
midpoints = bins[:-1] + np.diff(bins) / 2
cax.barh(midpoints, counts, height=np.diff(bins), color=cmap(norm(midpoints)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why are you making the width of the bars (which is the height) equal to the difference of the bins?

I use it in midpoints to center the bars between ticks that are labeled with the bin edges.

@livlutz
Copy link
Author

Thanks a lot for all the suggestions, I tried to see the changes locally like@rcomer said and it should look much better now, please let me know if there's still something missing

@story645
Copy link
Member

story645 commentedMay 26, 2025
edited
Loading

Can you please try the dataset@jklymak suggested in the original issue?

Also can you remove the spines (code should also be in the original example). Also please remove the yaxis_inversion as that's confusing me here - it might be clearer if you add the colorbar like in the original issue. Which also setting the margins to 0 (see original example) will remove the spacing for the bar chart so that it takes up the whole space (also please set height to 100% so they line up) - you are welcome to add that as a comment if it makes it clearer and to ask about the purpose of any code you don't understand.

@livlutz
Copy link
Author

I tried the first example from here :https://matplotlib.org/devdocs/gallery/images_contours_and_fields/image_demo.html and it worked just fine! please tell me if it is alright now

@story645
Copy link
Member

story645 commentedMay 26, 2025
edited
Loading

The new dataset isn't a normal distribution so the histogram should also not be a normal distribution. Please check your bins and extents (for example the dark blue and dark orange should be more prominent).

sphx_glr_colorbar_histogram_001_2_00x.png

Please plot the histogram without preset bins so that you can see what it's supposed to look like and then follow the example in the original issue for visualizing the correct histogram. Please ask questions if you don't understand the code.

Copy link
Member

@story645story645 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Also please remove the spines and margins from the histogram.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You should just use the ax.inset_axes method directly, please see the original issue for an example of doing so


# Histogram values and bars
midpoints = bins[:-1] + np.diff(bins) / 2
bar_height = np.min(np.diff(bins))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

1/len(counts) works as well - this width isn't supposed to be related to the data

cax.set_yticks(bins)

# Leave room for histogram inset
plt.subplots_adjust(right=0.75)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If you use the ax.inset_axes from the original than you won't need to use this

@livlutz
Copy link
Author

The new dataset isn't a normal distribution so the histogram should also not be a normal distribution. Please check your bins and extents (for example the dark blue and dark orange should be more prominent).

sphx_glr_colorbar_histogram_001_2_00x.png

Please plot the histogram without preset bins so that you can see what it's supposed to look like and then follow the example in the original issue for visualizing the correct histogram. Please ask questions if you don't understand the code.

I took a look at the original issue but it says the new dataset is a bivariate normal distribution, should I still change the histogram? And I'm a bit confused about the extents, do you have any suggestions on what changes should I make?

…, improve inset axes layout, and enhance label spacing
@livlutz
Copy link
Author

I changed the histogram slightly so it depicts a bivariate normal distribuition, please tell me if I still need to fix anything

@livlutz
Copy link
Author

Yes and it looks slightly different from the output I got when I ran the program locally...does it have to do with the output screen scale?

@livlutz
Copy link
Author

I made some adjustments to the positions of the image and the histogram, it should look a bit better now, I also tried the changes locally and it works just fine!

# === Plot Histogram ===
midpoints = bins[:-1] + np.diff(bins) / 2
bar_height = 1 / len(counts)
cax.barh(midpoints, counts, height=bar_height, color=cmap(norm(midpoints)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The bars are too thin to see properly. Suggest something like:

Suggested change
cax.barh(midpoints,counts,height=bar_height,color=cmap(norm(midpoints)))
cax.barh(midpoints,counts,height=np.median(np.diff(bins))*0.8,color=cmap(norm(midpoints)))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The bars are too thin to see properly. Suggest something like:

It was indeed too thin, it should be better now

@jklymak
Copy link
Member

I'd also figure out a distribution that had more features in the histogram. Also it is quite strange to have high values be blue and low values be red. (Yes I know the color map is upside down, but I'd just use the reversed version)

@livlutz
Copy link
Author

So sorry about the last 2 commits, I had some version conflicts in my branch and I was just trying to solve it. I made some changes suggested by@story645 and now it is showing the none loop version

@jklymak
Copy link
Member

I found something like

Z1=np.exp(-((X+1)*1.3)**2- ((Y+1)*1.3)**2)Z2=2.5*np.exp(-(X-1)**2- (Y-1)**2)Z= (Z1**0.25-Z2**0.5)

gave a much more interesting histogram.

I would also strongly suggestcmap = plt.get_cmap('RdYlBu_r') - blue for positive and red for negative is very unconventional.

@livlutz
Copy link
Author

The new histogram is in fact very interesting, should we keep it as the example?

# === Plot Histogram ===
midpoints = bins[:-1] + np.diff(bins) / 2
bar_height = 1 / len(counts)
cax.barh(midpoints, counts, height=np.median(np.diff(bins))*0.8, color=cmap(norm(midpoints)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Note you need to fix these lines that are too long and the trailing whitespace error.


# === Surface Data ===
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I zoomed this in a bit more to -2, +2, but not a big deal.


# === Main Plot ===
fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation='bilinear', cmap=cmap,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
im=ax.imshow(Z,interpolation='bilinear',cmap=cmap,
im=ax.imshow(Z,cmap=cmap,

I'm not sure we need or want interpolation here?

Comment on lines 38 to 41
plt.subplots_adjust(right=0.78, top=0.92, bottom=0.08)

# === Inset Histogram – Positioning adjusted ===
cax = ax.inset_axes([1.18, 0.02, 0.25, 0.95]) # left, bottom, width, height
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Our of curiosity: Wouldn't it be simpler to useplt.subplots(1, 2, width_ratios=...) instead of manually adjusting the axes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I find it easier to just manually set the width, but here mostly it's cause most of the use cases for this would I think use inset_axes

norm=norm)

# Adjust image position to allow space
plt.subplots_adjust(right=0.78, top=0.92, bottom=0.08)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is this actually doing anything anymore? I think there's technically only one subplot in this image?

@story645
Copy link
Member

The new histogram is in fact very interesting, should we keep it as the example?

I think it's cool, but what happens if you try like 21 bins? Basically, what's the minimum number of bins you need to have an interesting shape?

livlutzand others added2 commitsMay 27, 2025 21:54
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
@livlutz
Copy link
Author

The new histogram is in fact very interesting, should we keep it as the example?

I think it's cool, but what happens if you try like 21 bins? Basically, what's the minimum number of bins you need to have an interesting shape?

I see...so should we increase the number of bins we are using or do you think it would be better to go back to the previous histogram?

@jklymak
Copy link
Member

The new histogram is in fact very interesting, should we keep it as the example?

I think it's cool, but what happens if you try like 21 bins? Basically, what's the minimum number of bins you need to have an interesting shape?

I'm not sure the context for this question. In general, you want as many bins as you can get without having too many singleton bins.

@story645
Copy link
Member

story645 commentedMay 28, 2025
edited
Loading

I'm not sure the context for this question. In general, you want as many bins as you can get without having too many singleton bins.

Just that for the visualization it would maybe be less busy if there are fewer bins but it's not a big deal/blocker.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@story645story645story645 left review comments

@jklymakjklymakjklymak left review comments

@timhoffmtimhoffmtimhoffm left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
Documentation: examplesfiles in galleries/examples
Projects
Status: Needs review
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[Doc]: add histogram as colorbar example
5 participants
@livlutz@rcomer@story645@jklymak@timhoffm

[8]ページ先頭

©2009-2025 Movatter.jp