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 17 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
a49fac5
Rename evans_test.py example for Foo units class and conversion
livlutz May 24, 2025
87e8e37
Rename test_evans to unit_conversion_for_class_Foo
livlutz May 24, 2025
901cfa6
Merge branch 'matplotlib:main' into main
livlutz May 25, 2025
4df2b39
back to evans_test.py
livlutz May 25, 2025
646be10
Add colorbar histogram example to README and implement the example sc…
livlutz May 25, 2025
c81072f
Add completion message to colorbar histogram example
livlutz May 25, 2025
19a2d19
Fix newline at end of file in colorbar histogram example
livlutz May 25, 2025
9931dba
Refactor colorbar histogram example to use constrained layout and imp…
livlutz May 25, 2025
578e414
Refactor colorbar histogram example to improve layout and streamline …
livlutz May 25, 2025
0089861
Update colorbar histogram example: rename title, improve inset axes l…
livlutz May 26, 2025
bf6f2e8
Plotting a different database + refactor bins to np.linespace(-1,1,11…
livlutz May 26, 2025
9c5c859
Refactor colorbar histogram example: streamline histogram calculation…
livlutz May 26, 2025
96781f3
Refactor colorbar histogram example: adjust inset histogram positioni…
livlutz May 26, 2025
6062d91
Update histogram bar height calculation for improved visualization
livlutz May 27, 2025
69a388f
Update colorbar_histogram.py
livlutz May 27, 2025
b3d7b42
Refactor inset histogram cleanup: remove unnecessary comment and enha…
livlutz May 27, 2025
8004742
Refactor inset histogram cleanup: remove unnecessary comment to enhan…
livlutz May 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions 62 galleries/examples/color/colorbar_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
=========================
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(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

# === Histogram from actual Z data ===
counts, bins = np.histogram(Z, bins=30)

# === Colormap & Normalization ===
cmap = plt.get_cmap('RdYlBu')
norm = mcolors.BoundaryNorm(bins, cmap.N)

# === Main Plot ===
fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation='bilinear', cmap=cmap,
origin='lower', extent=[-3, 3, -3, 3],
norm=norm)

# Adjust image position to allow space
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

# === 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)))

Check failure on line 46 in galleries/examples/color/colorbar_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Line too long (93 > 88) Raw Output: message:"Line too long (93 > 88)" location:{path:"/home/runner/work/matplotlib/matplotlib/galleries/examples/color/colorbar_histogram.py" range:{start:{line:46 column:89} end:{line:46 column:94}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E501" url:"https://docs.astral.sh/ruff/rules/line-too-long"}

# === 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)

Check failure on line 55 in galleries/examples/color/colorbar_histogram.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/galleries/examples/color/colorbar_histogram.py" range:{start:{line:55 column:36} end:{line:55 column:37}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:55 column:36} end:{line:55 column:37}}}

# === Ticks ===
cax.set_yticks(bins)
cax.yaxis.set_major_locator(MaxNLocator(nbins=8))

plt.show()

Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.