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

Commit 578e414

Browse filesBrowse files
committed
Refactor colorbar histogram example to improve layout and streamline code
1 parent 9931dba commit 578e414
Copy full SHA for 578e414

File tree

1 file changed

+17
-25
lines changed
Filter options

1 file changed

+17
-25
lines changed

‎galleries/examples/color/colorbar_histogram.py

Copy file name to clipboardExpand all lines: galleries/examples/color/colorbar_histogram.py
+17-25Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,35 @@
1212
import numpy as np
1313
import matplotlib.pyplot as plt
1414
import matplotlib.colors as mcolors
15-
from matplotlib import gridspec
15+
from mpl_toolkits.axes_grid1 import make_axes_locatable
1616

1717
# Generate random data
18-
x = np.random.random(100).reshape(10, 10)
18+
x = np.random.random((10, 10))
1919

2020
# Compute histogram
2121
counts, bins = np.histogram(x)
2222

2323
# Set up colormap and normalization
24-
cmap = plt.colormaps['viridis']
24+
cmap = plt.get_cmap('viridis')
2525
norm = mcolors.BoundaryNorm(bins, cmap.N)
2626

27-
# Create figure with constrained_layout for better spacing
28-
fig = plt.figure(figsize=(8, 4), constrained_layout=True)
29-
gs = gridspec.GridSpec(1, 3, width_ratios=[4, 0.2, 1], figure=fig)
30-
31-
# Main image
32-
ax_img = fig.add_subplot(gs[0])
33-
im = ax_img.imshow(x, cmap=cmap, norm=norm)
34-
ax_img.set_title("Image")
35-
36-
# Colorbar
37-
cax = fig.add_subplot(gs[1])
38-
cbar = plt.colorbar(im, cax=cax)
27+
fig, ax = plt.subplots()
28+
im = ax.imshow(x, cmap=cmap, norm=norm)
29+
cbar = plt.colorbar(im, ax=ax)
3930
cbar.set_label('Value')
4031

41-
# Histogram
42-
ax_hist = fig.add_subplot(gs[2])
32+
# 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.
33+
divider = make_axes_locatable(ax)
34+
cax = divider.append_axes("right", size="20%", pad=0.05)
35+
36+
# Plot histogram
4337
midpoints = bins[:-1] + np.diff(bins) / 2
44-
ax_hist.barh(midpoints, counts, height=np.diff(bins), color=cmap(norm(midpoints)), edgecolor='k')
45-
ax_hist.set_yticks(bins)
46-
ax_hist.set_xlabel('Count')
47-
ax_hist.set_ylabel('Value')
48-
ax_hist.margins(0)
49-
for spine in ax_hist.spines.values():
50-
spine.set_visible(False)
38+
cax.barh(midpoints, counts, height=np.diff(bins), color=cmap(norm(midpoints)))
39+
cax.set_yticks(bins)
40+
cax.set_xlabel('Count')
41+
cax.set_ylabel('Value')
42+
cax.invert_yaxis() # Optional: to match the orientation of imshow
5143

44+
plt.tight_layout()
5245
plt.show()
5346

54-
print("Colorbar with histogram example completed.")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.