Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
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
base:main
Are you sure you want to change the base?
Conversation
@story645 thanks for the suggestion! This PR is ready for review |
It looks like something went wrong with the layout. Also, please read the comments on the issue for ideas how to improve this. |
livlutz commentedMay 25, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 commentedMay 25, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. |
story645 left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
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 .
galleries/examples/color/README.txt Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
- 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
ColorbarwithHistogram | |
HistogramasColorbar |
This example is about using a histogram in place of a regular colorbar
# 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) |
There was a problem hiding this comment.
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))) |
There was a problem hiding this comment.
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.
…ayout, and adjust label positions
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 commentedMay 26, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. |
…) + remove yaxis inversion
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 commentedMay 26, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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). 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. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
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
I changed the histogram slightly so it depicts a bivariate normal distribuition, please tell me if I still need to fix anything |
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? |
…ng, improve layout, and clean up axis labels
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))) |
There was a problem hiding this comment.
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:
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))) |
There was a problem hiding this comment.
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
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) |
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: hannah <story645@gmail.com>
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 |
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 suggest |
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))) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
im=ax.imshow(Z,interpolation='bilinear',cmap=cmap, | |
im=ax.imshow(Z,cmap=cmap, |
I'm not sure we need or want interpolation here?
…ove interpolation in imshow
Uh oh!
There was an error while loading.Please reload this page.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
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? |
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
…ar histogram example
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? |
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 commentedMay 28, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Just that for the visualization it would maybe be less busy if there are fewer bins but it's not a big deal/blocker. |
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