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

Commitcf526b1

Browse files
authored
Merge pull request#11598 from matplotlib/auto-backport-of-pr-10915-on-v2.2.x
Backport PR#10915 on branch v2.2.x
2 parents5b1a052 +6fe8965 commitcf526b1

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

‎lib/matplotlib/tests/test_tightlayout.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,41 @@ def test_empty_layout():
272272

273273
fig=plt.gcf()
274274
fig.tight_layout()
275+
276+
277+
deftest_verybig_decorators_horizontal():
278+
"Test that warning emitted when xlabel too big"
279+
fig,ax=plt.subplots(figsize=(3,2))
280+
ax.set_xlabel('a'*100)
281+
withwarnings.catch_warnings(record=True)asw:
282+
fig.tight_layout()
283+
assertlen(w)==1
284+
285+
286+
deftest_verybig_decorators_vertical():
287+
"Test that warning emitted when xlabel too big"
288+
fig,ax=plt.subplots(figsize=(3,2))
289+
ax.set_ylabel('a'*100)
290+
withwarnings.catch_warnings(record=True)asw:
291+
fig.tight_layout()
292+
assertlen(w)==1
293+
294+
295+
deftest_big_decorators_horizontal():
296+
"Test that warning emitted when xlabel too big"
297+
fig,axs=plt.subplots(1,2,figsize=(3,2))
298+
axs[0].set_xlabel('a'*30)
299+
axs[1].set_xlabel('b'*30)
300+
withwarnings.catch_warnings(record=True)asw:
301+
fig.tight_layout()
302+
assertlen(w)==1
303+
304+
305+
deftest_big_decorators_vertical():
306+
"Test that warning emitted when xlabel too big"
307+
fig,axs=plt.subplots(2,1,figsize=(3,2))
308+
axs[0].set_ylabel('a'*20)
309+
axs[1].set_ylabel('b'*20)
310+
withwarnings.catch_warnings(record=True)asw:
311+
fig.tight_layout()
312+
assertlen(w)==1

‎lib/matplotlib/tight_layout.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,46 @@ def auto_adjust_subplotpars(
171171
margin_bottom=max([sum(s)forsinvspaces[-cols:]]+ [0])
172172
margin_bottom+=pad_inches/fig_height_inch
173173

174+
ifmargin_left+margin_right>=1:
175+
margin_left=0.4999
176+
margin_right=0.4999
177+
warnings.warn('The left and right margins cannot be made large '
178+
'enough to accommodate all axes decorations. ')
179+
ifmargin_bottom+margin_top>=1:
180+
margin_bottom=0.4999
181+
margin_top=0.4999
182+
warnings.warn('The bottom and top margins cannot be made large '
183+
'enough to accommodate all axes decorations. ')
184+
174185
kwargs=dict(left=margin_left,
175186
right=1-margin_right,
176187
bottom=margin_bottom,
177188
top=1-margin_top)
178-
179189
ifcols>1:
180190
hspace= (
181191
max(sum(s)
182192
foriinrange(rows)
183193
forsinhspaces[i* (cols+1)+1:(i+1)* (cols+1)-1])
184194
+hpad_inches/fig_width_inch)
195+
# axes widths:
185196
h_axes= (1-margin_right-margin_left-hspace* (cols-1))/cols
186-
kwargs["wspace"]=hspace/h_axes
197+
ifh_axes<0:
198+
warnings.warn('tight_layout cannot make axes width small enough '
199+
'to accommodate all axes decorations')
200+
kwargs["wspace"]=0.5
201+
else:
202+
kwargs["wspace"]=hspace/h_axes
187203

188204
ifrows>1:
189205
vspace= (max(sum(s)forsinvspaces[cols:-cols])
190206
+vpad_inches/fig_height_inch)
191207
v_axes= (1-margin_top-margin_bottom-vspace* (rows-1))/rows
192-
kwargs["hspace"]=vspace/v_axes
208+
ifv_axes<0:
209+
warnings.warn('tight_layout cannot make axes height small enough '
210+
'to accommodate all axes decorations')
211+
kwargs["hspace"]=0.5
212+
else:
213+
kwargs["hspace"]=vspace/v_axes
193214

194215
returnkwargs
195216

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp