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
Open
livlutz wants to merge22 commits intomatplotlib:mainChoose a base branch fromlivlutz:histogram-colorgram
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+61 −0
Open
Changes fromall commits
Commits
Show all changes
22 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
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
61 changes: 61 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,61 @@ | ||
""" | ||
===================== | ||
Histogram as colorbar | ||
===================== | ||
This example demonstrates how to create a colorbar for an image and | ||
add a histogram of the data values alongside it. This is useful for | ||
visualizing the distribution of values mapped to colors. | ||
""" | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import matplotlib.colors as mcolors | ||
from matplotlib.ticker import MaxNLocator | ||
# === 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) | ||
# === Histogram from actual Z data === | ||
counts, bins = np.histogram(Z, bins=30) | ||
# === Colormap & Normalization === | ||
cmap = plt.get_cmap('RdYlBu_r') | ||
norm = mcolors.BoundaryNorm(bins, 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 – Positioning adjusted === | ||
cax = ax.inset_axes([1.18, 0.02, 0.25, 0.95]) # left, bottom, width, height | ||
# === Plot Histogram === | ||
midpoints = bins[:-1] + np.diff(bins) / 2 | ||
height = np.median(np.diff(bins)) * 0.8 | ||
colors = cmap(norm(midpoints)) | ||
cax.barh(midpoints, counts, height=height, color=colors) | ||
# === Clean up === | ||
cax.spines[:].set_visible(False) | ||
cax.margins(0) | ||
cax.tick_params(axis='both', which='both', length=0) | ||
# === Axis labels === | ||
cax.set_xlabel('Count', labelpad=10) | ||
cax.set_ylabel('Value', labelpad=6) | ||
# === Ticks === | ||
cax.set_yticks(bins) | ||
cax.yaxis.set_major_locator(MaxNLocator(nbins=8)) | ||
plt.show() | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.