Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitb67df2d

Browse files
authored
Merge pull request#10657 from matplotlib/auto-backport-of-pr-10629
Backport PR#10629 on branch v2.2.x
2 parents98a0d68 +b49fcb2 commitb67df2d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

‎lib/matplotlib/colorbar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
11261126
anchor=kw.pop('anchor',loc_settings['anchor'])
11271127
parent_anchor=kw.pop('panchor',loc_settings['panchor'])
11281128

1129+
parents_iterable=cbook.iterable(parents)
11291130
# turn parents into a list if it is not already. We do this w/ np
11301131
# because `plt.subplots` can return an ndarray and is natural to
11311132
# pass to `colorbar`.
@@ -1193,7 +1194,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
11931194
# and we need to set the aspect ratio by hand...
11941195
cax.set_aspect(aspect,anchor=anchor,adjustable='box')
11951196
else:
1196-
iflen(parents)==1:
1197+
ifnotparents_iterable:
11971198
# this is a single axis...
11981199
ax=parents[0]
11991200
lb,lbpos=constrained_layout.layoutcolorbarsingle(

‎tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,39 @@ def example_plot(ax, fontsize=12, nodec=False):
105105
fig.colorbar(im,ax=ax,shrink=0.6)
106106

107107
############################################################################
108-
# If you specify multiple axes to the ``ax`` argument of ``colorbar``,
109-
# constrained_layout will take space from all axes that share the same
110-
# gridspec.
108+
# If you specify a list of axes (or other iterable container) to the
109+
# ``ax`` argument of ``colorbar``, constrained_layout will take space from all # axes that share the same gridspec.
111110

112111
fig,axs=plt.subplots(2,2,figsize=(4,4),constrained_layout=True)
113112
foraxinaxs.flatten():
114113
im=ax.pcolormesh(arr,rasterized=True)
115114
fig.colorbar(im,ax=axs,shrink=0.6)
116115

116+
############################################################################
117+
# Note that there is a bit of a subtlety when specifying a single axes
118+
# as the parent. In the following, it might be desirable and expected
119+
# for the colorbars to line up, but they don't because the colorbar paired
120+
# with the bottom axes is tied to the subplotspec of the axes, and hence
121+
# shrinks when the gridspec-level colorbar is added.
122+
123+
fig,axs=plt.subplots(3,1,figsize=(4,4),constrained_layout=True)
124+
foraxinaxs[:2]:
125+
im=ax.pcolormesh(arr,rasterized=True)
126+
fig.colorbar(im,ax=axs[:2],shrink=0.6)
127+
im=axs[2].pcolormesh(arr,rasterized=True)
128+
fig.colorbar(im,ax=axs[2],shrink=0.6)
129+
130+
############################################################################
131+
# The API to make a single-axes behave like a list of axes is to specify
132+
# it as a list (or other iterable container), as below:
133+
134+
fig,axs=plt.subplots(3,1,figsize=(4,4),constrained_layout=True)
135+
foraxinaxs[:2]:
136+
im=ax.pcolormesh(arr,rasterized=True)
137+
fig.colorbar(im,ax=axs[:2],shrink=0.6)
138+
im=axs[2].pcolormesh(arr,rasterized=True)
139+
fig.colorbar(im,ax=[axs[2]],shrink=0.6)
140+
117141
####################################################
118142
# Suptitle
119143
# =========

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp