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

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
Loading
from

Conversation

livlutz
Copy link

@livlutz livlutz commented May 25, 2025

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-actions github-actions bot added the Documentation: examples files in galleries/examples label May 25, 2025
@livlutz
Copy link
Author

livlutz commented May 25, 2025

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

@rcomer
Copy link
Member

rcomer commented May 25, 2025

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

@livlutz
Copy link
Author

livlutz commented May 25, 2025

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 commented May 25, 2025

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

@story645 story645 left a 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 .

@@ -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
Colorbar with Histogram
Histogram as Colorbar

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

livlutz commented May 26, 2025

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 commented May 26, 2025

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

livlutz commented May 26, 2025

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 commented May 26, 2025

The new dataset is bimodal so the histogram should be bimodal. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: examples files in galleries/examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Doc]: add histogram as colorbar example
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.