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

Commit41727bd

Browse files
committed
DOC: document new inbbox where appropriate
1 parenta570cc9 commit41727bd

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

‎lib/matplotlib/axes/_base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,6 +4096,11 @@ def pick(self, *args):
40964096
martist.Artist.pick(self,args[0])
40974097

40984098
defget_default_bbox_extra_artists(self):
4099+
"""
4100+
Return a default list of artists that are used for the bounding box
4101+
calculation. Artists are excluded either by not being visible
4102+
or ``artist.inbbox = False``.
4103+
"""
40994104
return [artistforartistinself.get_children()
41004105
if (artist.get_visible()andartist.inbbox)]
41014106

@@ -4105,8 +4110,8 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41054110
Return the tight bounding box of the axes, including axis and their
41064111
decorators (xlabel, title, etc).
41074112
4108-
Artists that have ``artist.inbbox=False`` are not included
4109-
in the bbox.
4113+
Artists that have ``artist.inbbox =False`` are not included
4114+
in the bbox.
41104115
41114116
Parameters
41124117
----------

‎lib/matplotlib/figure.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,9 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None):
20022002
"""
20032003
Return a (tight) bounding box of the figure in inches.
20042004
2005+
Artists that have ``artist.inbbox = False`` are not included
2006+
in the bbox.
2007+
20052008
Parameters
20062009
----------
20072010
renderer : `.RendererBase` instance
@@ -2084,6 +2087,10 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
20842087
"""
20852088
Automatically adjust subplot parameters to give specified padding.
20862089
2090+
To exclude an artist on the axes from the bounding box calculation
2091+
that determines the subplot parameters (i.e. legend, or annotation),
2092+
then set `a.inbbox=False` for that artist.
2093+
20872094
Parameters
20882095
----------
20892096
pad : float

‎tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def example_plot(ax, fontsize=12, nodec=False):
172172

173173
fig,ax=plt.subplots(constrained_layout=True)
174174
ax.plot(np.arange(10),label='This is a plot')
175-
ax.legend(loc='center left',bbox_to_anchor=(0.9,0.5))
175+
ax.legend(loc='center left',bbox_to_anchor=(0.8,0.5))
176176

177177
#############################################
178178
# However, this will steal space from a subplot layout:
@@ -181,7 +181,39 @@ def example_plot(ax, fontsize=12, nodec=False):
181181
foraxinaxs.flatten()[:-1]:
182182
ax.plot(np.arange(10))
183183
axs[1,1].plot(np.arange(10),label='This is a plot')
184-
axs[1,1].legend(loc='center left',bbox_to_anchor=(0.9,0.5))
184+
axs[1,1].legend(loc='center left',bbox_to_anchor=(0.8,0.5))
185+
186+
#############################################
187+
# In order for a legend or other artist to *not* steal space
188+
# from the subplot layout, it can have its ``inbbox`` property
189+
# set to ``False``. Of course this can mean the legend ends up
190+
# cropped, but can be useful if the plot is subsequently called
191+
# with ``fig.savefig('outname.png', bbox_inches='tight')``. Note,
192+
# however, that the legend's inbbox status will have to be
193+
# toggled again to make the saved file work:
194+
195+
fig,axs=plt.subplots(2,2,constrained_layout=True)
196+
foraxinaxs.flatten()[:-1]:
197+
ax.plot(np.arange(10))
198+
axs[1,1].plot(np.arange(10),label='This is a plot')
199+
leg=axs[1,1].legend(loc='center left',bbox_to_anchor=(0.8,0.5))
200+
leg.inbbox=False
201+
wanttoprint=False
202+
ifwanttoprint:
203+
leg.inbbox=True
204+
fig.do_constrained_layout(False)
205+
fig.savefig('outname.png',bbox_inches='tight')
206+
207+
#############################################
208+
# A better way to get around this awkwardness is to simply
209+
# use a legend for the figure:
210+
fig,axs=plt.subplots(2,2,constrained_layout=True)
211+
foraxinaxs.flatten()[:-1]:
212+
ax.plot(np.arange(10))
213+
lines=axs[1,1].plot(np.arange(10),label='This is a plot')
214+
labels= [l.get_label()forlinlines]
215+
leg=fig.legend(lines,labels,loc='center left',
216+
bbox_to_anchor=(0.8,0.5),bbox_transform=axs[1,1].transAxes)
185217

186218
###############################################################################
187219
# Padding and Spacing

‎tutorials/intermediate/tight_layout_guide.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
feature and may not work for some cases. It only checks the extents
1111
of ticklabels, axis labels, and titles.
1212
13+
See also constrained_layout for a different way to do the same things as
14+
tight-layout:
15+
:doc:`/tutorials/intermediate/constrainedlayout_guide`
16+
1317
1418
Simple Example
1519
==============
@@ -280,6 +284,35 @@ def example_plot(ax, fontsize=12):
280284
None,1- (gs2.top-top)],
281285
h_pad=0.5)
282286

287+
###############################################################################
288+
# Legends and Annotations
289+
# =======================
290+
#
291+
# Pre Matplotlih 2.2, legends and annotations were excluded from the bounding
292+
# box calculations that decide the layout. Subsequently these artists were
293+
# added to the calculation, but sometimes it is undesirable to include them.
294+
# For instance in this case it might be good to have the axes shring a bit
295+
# to make room for the legend:
296+
297+
fig,ax=plt.subplots(figsize=(4,3))
298+
lines=ax.plot(range(10),label='A simple plot')
299+
ax.legend(bbox_to_anchor=(0.7,0.5),loc='center left',)
300+
fig.tight_layout()
301+
plt.show()
302+
303+
###############################################################################
304+
# However, sometimes this is not desired (quite often when using
305+
# ``fig.savefig('outname.png', bbox_inches='tight')``). In order to
306+
# remove the legend from the bounding box calculation, we simply set its
307+
# bounding ``inbbox=False`` and the legend will be ignored.
308+
309+
fig,ax=plt.subplots(figsize=(4,3))
310+
lines=ax.plot(range(10),label='B simple plot')
311+
leg=ax.legend(bbox_to_anchor=(0.7,0.5),loc='center left',)
312+
leg.inbbox=False
313+
fig.tight_layout()
314+
plt.show()
315+
283316
###############################################################################
284317
# Use with AxesGrid1
285318
# ==================

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp