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

Commit474a90c

Browse files
committed
FIX: long titles x/ylabel layout
1 parent68652b1 commit474a90c

File tree

8 files changed

+82
-23
lines changed

8 files changed

+82
-23
lines changed

‎doc/api/api_changes_3.3/behaviour.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ no longer accept the unsupported ``'best'`` location. Previously, invalid Axes
201201
locations would use ``'best'`` and invalid Figure locations would used ``'upper
202202
right'``.
203203

204-
205204
Passing Line2D's *drawstyle* together with *linestyle* is removed
206205
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207206

@@ -214,3 +213,18 @@ Upper case color strings
214213

215214
Support for passing single-letter colors (one of "rgbcmykw") as UPPERCASE
216215
characters is removed; these colors are now case-sensitive (lowercase).
216+
217+
tight/constrained_layout no longer worry about titles that are too wide
218+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219+
220+
*tight_layout* and *constrained_layout* shrink axes to accommodate
221+
"decorations" on the axes. However, if an xlabel or title is too long in the
222+
x direction, making the axes smaller in the x-direction doesn't help. The
223+
behavior of both has been changed to ignore the width of the title and
224+
xlabel and the height of the ylabel in the layout logic.
225+
226+
This also means there is a new keyword argument for `.axes.Axes.get_tightbbox`:
227+
``for_layout_only``, which defaults to *False*, but if *True* returns a
228+
bounding box using the rules above. `.axis.Axis.get_tightbbox` gets an
229+
``ignore_label`` keyword argument, which is *None* by default, but which can
230+
also be 'x' or 'y'.

‎lib/matplotlib/_constrained_layout.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ def _make_layout_margins(ax, renderer, h_pad, w_pad):
258258
fig=ax.figure
259259
invTransFig=fig.transFigure.inverted().transform_bbox
260260
pos=ax.get_position(original=True)
261-
tightbbox=ax.get_tightbbox(renderer=renderer)
261+
try:
262+
tightbbox=ax.get_tightbbox(renderer=renderer,for_layout_only=True)
263+
exceptTypeError:
264+
tightbbox=ax.get_tightbbox(renderer=renderer)
265+
262266
iftightbboxisNone:
263267
bbox=pos
264268
else:

‎lib/matplotlib/axes/_base.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,11 +4086,15 @@ def get_default_bbox_extra_artists(self):
40864086
for_axisinself._get_axis_list():
40874087
artists.remove(_axis)
40884088

4089+
artists.remove(self.title)
4090+
artists.remove(self._left_title)
4091+
artists.remove(self._right_title)
4092+
40894093
return [artistforartistinartists
40904094
if (artist.get_visible()andartist.get_in_layout())]
40914095

40924096
defget_tightbbox(self,renderer,call_axes_locator=True,
4093-
bbox_extra_artists=None):
4097+
bbox_extra_artists=None,*,for_layout_only=False):
40944098
"""
40954099
Return the tight bounding box of the axes, including axis and their
40964100
decorators (xlabel, title, etc).
@@ -4116,6 +4120,10 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41164120
caller is only interested in the relative size of the tightbbox
41174121
compared to the axes bbox.
41184122
4123+
for_layout_only : default: False
4124+
The bounding box will *not* include the x-extent of the title and
4125+
the xlabel, or the y-extent of the ylabel.
4126+
41194127
Returns
41204128
-------
41214129
`.BboxBase`
@@ -4141,22 +4149,37 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41414149
self.apply_aspect()
41424150

41434151
ifself.axison:
4144-
bb_xaxis=self.xaxis.get_tightbbox(renderer)
4152+
igl='x'iffor_layout_onlyelseNone
4153+
try:
4154+
bb_xaxis=self.xaxis.get_tightbbox(renderer,ignore_label=igl)
4155+
exceptTypeError:
4156+
# in case downstream library has redefined axis:
4157+
bb_xaxis=self.xaxis.get_tightbbox(renderer)
41454158
ifbb_xaxis:
41464159
bb.append(bb_xaxis)
41474160

4148-
bb_yaxis=self.yaxis.get_tightbbox(renderer)
4161+
igl='y'iffor_layout_onlyelseNone
4162+
try:
4163+
bb_yaxis=self.yaxis.get_tightbbox(renderer,ignore_label=igl)
4164+
exceptTypeError:
4165+
# in case downstream library has redefined axis:
4166+
bb_xaxis=self.yaxis.get_tightbbox(renderer)
41494167
ifbb_yaxis:
41504168
bb.append(bb_yaxis)
4151-
41524169
self._update_title_position(renderer)
4153-
41544170
axbbox=self.get_window_extent(renderer)
41554171
bb.append(axbbox)
41564172

41574173
fortitlein [self.title,self._left_title,self._right_title]:
41584174
iftitle.get_visible():
4159-
bb.append(title.get_window_extent(renderer))
4175+
bt=title.get_window_extent(renderer)
4176+
iffor_layout_onlyandbt.width>0:
4177+
# make the title bbox 1 pixel wide so its width
4178+
# is not accounted for in bbox calculations in
4179+
# tight/constrained_layout
4180+
bt.x0= (bt.x0+bt.x1)/2-0.5
4181+
bt.x1=bt.x0+1.0
4182+
bb.append(bt)
41604183

41614184
bbox_artists=bbox_extra_artists
41624185
ifbbox_artistsisNone:
@@ -4179,7 +4202,6 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41794202
and0<bbox.width<np.inf
41804203
and0<bbox.height<np.inf):
41814204
bb.append(bbox)
4182-
41834205
returnmtransforms.Bbox.union(
41844206
[bforbinbbifb.width!=0orb.height!=0])
41854207

‎lib/matplotlib/axis.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,10 +1079,15 @@ def _get_tick_bboxes(self, ticks, renderer):
10791079
[tick.label2.get_window_extent(renderer)
10801080
fortickinticksiftick.label2.get_visible()])
10811081

1082-
defget_tightbbox(self,renderer):
1082+
defget_tightbbox(self,renderer,*,ignore_label=None):
10831083
"""
10841084
Return a bounding box that encloses the axis. It only accounts
10851085
tick labels, axis label, and offsetText.
1086+
1087+
If ``ignore_label`` is 'x', then the width of the label is collapsed
1088+
to near zero. If 'y', then the height is collapsed to near zero. This
1089+
is for tight/constrained_layout to be able to ignore too-long labels
1090+
when doing their layout.
10861091
"""
10871092
ifnotself.get_visible():
10881093
return
@@ -1100,11 +1105,24 @@ def get_tightbbox(self, renderer):
11001105

11011106
bboxes= [
11021107
*(a.get_window_extent(renderer)
1103-
forain [self.label,self.offsetText]
1108+
forain [self.offsetText]
11041109
ifa.get_visible()),
11051110
*ticklabelBoxes,
11061111
*ticklabelBoxes2,
11071112
]
1113+
# take care of label
1114+
ifself.label.get_visible():
1115+
bb=self.label.get_window_extent(renderer)
1116+
# for constrained/tight_layout, we want to ignore the label's
1117+
# width because the adjustments they make can't be improved.
1118+
# this code collapses the relevant direction
1119+
ifignore_label=='x'andbb.width>0:
1120+
bb.x0= (bb.x0+bb.x1)/2-0.5
1121+
bb.x1=bb.x0+1.0
1122+
elifignore_label=='y'andbb.height>0:
1123+
bb.y0= (bb.y0+bb.y1)/2-0.5
1124+
bb.y1=bb.y0+1.0
1125+
bboxes.append(bb)
11081126
bboxes= [bforbinbboxes
11091127
if0<b.width<np.infand0<b.height<np.inf]
11101128
ifbboxes:

‎lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_constrained_layout10():
176176
@image_comparison(['constrained_layout11.png'])
177177
deftest_constrained_layout11():
178178
"""Test for multiple nested gridspecs"""
179-
fig=plt.figure(constrained_layout=True,figsize=(10,3))
179+
fig=plt.figure(constrained_layout=True,figsize=(13,3))
180180
gs0=gridspec.GridSpec(1,2,figure=fig)
181181
gsl=gridspec.GridSpecFromSubplotSpec(1,2,gs0[0])
182182
gsl0=gridspec.GridSpecFromSubplotSpec(2,2,gsl[1])

‎lib/matplotlib/tests/test_tightlayout.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,23 @@ def test_empty_layout():
258258

259259
@pytest.mark.parametrize("label", ["xlabel","ylabel"])
260260
deftest_verybig_decorators(label):
261-
"""Test that warning emitted when xlabel/ylabel too big."""
261+
"""Test thatnowarning emitted when xlabel/ylabel too big."""
262262
fig,ax=plt.subplots(figsize=(3,2))
263263
ax.set(**{label:'a'*100})
264-
withpytest.warns(UserWarning):
265-
fig.tight_layout()
266264

267265

268266
deftest_big_decorators_horizontal():
269-
"""Test thatwarning emitted when xlabel too big."""
267+
"""Test thatdoesn't warn when xlabel too big."""
270268
fig,axs=plt.subplots(1,2,figsize=(3,2))
271269
axs[0].set_xlabel('a'*30)
272270
axs[1].set_xlabel('b'*30)
273-
withpytest.warns(UserWarning):
274-
fig.tight_layout()
275271

276272

277273
deftest_big_decorators_vertical():
278-
"""Test thatwarning emitted whenxlabel too big."""
274+
"""Test thatdoesn't warn whenylabel too big."""
279275
fig,axs=plt.subplots(2,1,figsize=(3,2))
280276
axs[0].set_ylabel('a'*20)
281277
axs[1].set_ylabel('b'*20)
282-
withpytest.warns(UserWarning):
283-
fig.tight_layout()
284278

285279

286280
deftest_badsubplotgrid():

‎lib/matplotlib/tight_layout.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,15 @@ def auto_adjust_subplotpars(
7777
ifall(notax.get_visible()foraxinsubplots):
7878
continue
7979

80-
tight_bbox_raw=Bbox.union([
81-
ax.get_tightbbox(renderer)foraxinsubplotsifax.get_visible()])
80+
bb= []
81+
foraxinsubplots:
82+
ifax.get_visible():
83+
try:
84+
bb+= [ax.get_tightbbox(renderer,for_layout_only=True)]
85+
exceptTypeError:
86+
bb+= [ax.get_tightbbox(renderer)]
87+
88+
tight_bbox_raw=Bbox.union(bb)
8289
tight_bbox=TransformedBbox(tight_bbox_raw,
8390
fig.transFigure.inverted())
8491

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp