Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
31 commits Select commitHold shift + click to select a range
a49fac5 Rename evans_test.py example for Foo units class and conversion
livlutz87e8e37 Rename test_evans to unit_conversion_for_class_Foo
livlutz901cfa6 Merge branch 'matplotlib:main' into main
livlutz4df2b39 back to evans_test.py
livlutz646be10 Add colorbar histogram example to README and implement the example sc…
livlutzc81072f Add completion message to colorbar histogram example
livlutz19a2d19 Fix newline at end of file in colorbar histogram example
livlutz9931dba Refactor colorbar histogram example to use constrained layout and imp…
livlutz578e414 Refactor colorbar histogram example to improve layout and streamline …
livlutz0089861 Update colorbar histogram example: rename title, improve inset axes l…
livlutzbf6f2e8 Plotting a different database + refactor bins to np.linespace(-1,1,11…
livlutz9c5c859 Refactor colorbar histogram example: streamline histogram calculation…
livlutz96781f3 Refactor colorbar histogram example: adjust inset histogram positioni…
livlutz6062d91 Update histogram bar height calculation for improved visualization
livlutz69a388f Update colorbar_histogram.py
livlutzb3d7b42 Refactor inset histogram cleanup: remove unnecessary comment and enha…
livlutz8004742 Refactor inset histogram cleanup: remove unnecessary comment to enhan…
livlutzf3e8acf Using a new histogram + swapping blue and red in cmap
livlutzcf4f62c Fix histogram range and improve image display: adjust x range and rem…
livlutz767ec0e Update galleries/examples/color/colorbar_histogram.py
livlutzfdb37d4 Remove unnecessary subplot adjustment for image positioning in colorb…
livlutz31cf67d Remove trailing whitespace + adding new layout as constrained
livlutze19c7c6 Merge branch 'matplotlib:main' into histogram-colorgram
livlutze02b014 Refactor histogram calculation and cleanup: simplify midpoints calcul…
livlutzbeda46a Adding newline at the end of file
livlutzaf473b9 organising imports with isort
livlutza41d546 Changing comment style + simpler instructions
livlutz685e2d1 Refactor example description and improve comment consistency in color…
livlutz09d65d5 Fix normalization and y-ticks in colorbar histogram example
livlutz0800664 Fix comment inconsistencies and remove unnecessary lines in colorbar_…
livlutz3ee4a67 Fix histogram bin edges when plotting histogram
livlutzFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletionsgalleries/examples/color/colorbar_histogram.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| """ | ||
| ===================== | ||
| Histogram as colorbar | ||
| ===================== | ||
| This example demonstrates how to use a colored histogram instead of a colorbar | ||
| to not only show the color-to-value mapping, but also visualize the | ||
| distribution of values. | ||
| """ | ||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| import matplotlib.colors as mcolors | ||
| # surface data | ||
| delta = 0.025 | ||
| x = y = np.arange(-2.0, 2.0, delta) | ||
| X, Y = np.meshgrid(x, y) | ||
| 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 | ||
| # colormap & normalization | ||
| bins = 30 | ||
| cmap = plt.get_cmap("RdYlBu_r") | ||
timhoffm marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| bin_edges = np.linspace(Z.min(), Z.max(), bins + 1) | ||
| norm = mcolors.BoundaryNorm(bin_edges, cmap.N) | ||
| # main plot | ||
| fig, ax = plt.subplots(layout="constrained") | ||
| im = ax.imshow(Z, cmap=cmap, origin="lower", extent=[-3, 3, -3, 3], norm=norm) | ||
| # inset histogram | ||
| cax = ax.inset_axes([1.18, 0.02, 0.25, 0.95]) # left, bottom, width, height | ||
| # plot histogram | ||
| counts, _ = np.histogram(Z, bins=bin_edges) | ||
| midpoints = (bin_edges[:-1] + bin_edges[1:]) / 2 | ||
| distance = midpoints[1] - midpoints[0] | ||
| cax.barh(midpoints, counts, height=0.8 * distance, color=cmap(norm(midpoints))) | ||
| # styling | ||
| cax.spines[:].set_visible(False) | ||
| cax.set_yticks(bin_edges) | ||
| cax.tick_params(axis="both", which="both", length=0) | ||
| plt.show() | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.