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 28ee515

Browse filesBrowse files
committed
DOC: add fixed-aspect colorbar examples
1 parent 7e3cef3 commit 28ee515
Copy full SHA for 28ee515

File tree

Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed

‎examples/subplots_axes_and_figures/colorbar_placement.py

Copy file name to clipboardExpand all lines: examples/subplots_axes_and_figures/colorbar_placement.py
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,46 @@
5353
fig.colorbar(pcm, ax=[axs[0, 2]], location='bottom')
5454
fig.colorbar(pcm, ax=axs[1:, :], location='right', shrink=0.6)
5555
fig.colorbar(pcm, ax=[axs[2, 1]], location='left')
56+
plt.show()
5657

58+
######################################################################
59+
# Colorbars with fixed-aspect-ratio axes
60+
# ======================================
61+
#
62+
# Placing colorbars for axes with a fixed aspect ratio pose a particular
63+
# challenge as the parent axes changes size depending on the data view.
5764

65+
fig, axs = plt.subplots(2, 2, constrained_layout=True)
66+
cmaps = ['RdBu_r', 'viridis']
67+
for col in range(2):
68+
for row in range(2):
69+
ax = axs[row, col]
70+
pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1),
71+
cmap=cmaps[col])
72+
if col == 0:
73+
ax.set_aspect(2)
74+
else:
75+
ax.set_aspect(1/2)
76+
if row == 1:
77+
fig.colorbar(pcm, ax=ax, shrink=0.6)
78+
plt.show()
79+
80+
# One way around this issue is to use an `.Axes.inset_axes` to locate the
81+
# axes in axes co-ordinates. Note that if you zoom in on the axes, and
82+
# change the shape of the axes, the colorbar will also change position.
83+
84+
fig, axs = plt.subplots(2, 2, constrained_layout=True)
85+
cmaps = ['RdBu_r', 'viridis']
86+
for col in range(2):
87+
for row in range(2):
88+
ax = axs[row, col]
89+
pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1),
90+
cmap=cmaps[col])
91+
if col == 0:
92+
ax.set_aspect(2)
93+
else:
94+
ax.set_aspect(1/2)
95+
if row == 1:
96+
cax = ax.inset_axes([1.04, 0.2, 0.05, 0.6], transform=ax.transAxes)
97+
fig.colorbar(pcm, ax=ax, cax=cax)
5898
plt.show()

0 commit comments

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