8000 ENH: change inbbox to in_layout and add setters/getters · matplotlib/matplotlib@7e5a28e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e5a28e

Browse files
committed
ENH: change inbbox to in_layout and add setters/getters
1 parent f7df3f9 commit 7e5a28e

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

doc/api/next_api_changes/2018-04-29-JMK.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ is to make the artist a child of the figure, not the axes. i.e. calling
1717
`~.matplotlib.Axes.get_legend_handles_labels` to gather handles and labels
1818
from the parent axes).
1919

20-
The second way is to set the artist property ``artist.inbbox`` to ``False`` to
21-
make the artist not be included in the bounding box calculation.
20+
The second way is to set the artist property ``artist.set_in_layout(False)``
21+
to make the artist not be included in the bounding box calculation.

lib/matplotlib/artist.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __init__(self):
119119
self._sketch = rcParams['path.sketch']
120120
self._path_effects = rcParams['path.effects']
121121
self._sticky_edges = _XYPair([], [])
122-
self.inbbox = True
122+
self._in_layout = True
123123

124124
def __getstate__(self):
125125
d = self.__dict__.copy()
@@ -743,6 +743,14 @@ def get_animated(self):
743743
"Return the artist's animated state"
744744
return self._animated
745745

746+
def get_in_layout(self):
747+
""""
748+
Return if artist is to be included in layout calculations
749+
like `.Figure.tight_layout` and constrained layout. Also
750+
for ``fig.savefig(fname, bbox_inches='tight').
751+
"""
752+
return self._in_layout
753+
746754
def get_clip_on(self):
747755
'Return whether artist uses clipping'
748756
return self._clipon
@@ -878,6 +886,18 @@ def set_animated(self, b):
878886
self._animated = b
879887
self.pchanged()
880888

889+
def set_in_layout(in_layout):
890+
""""
891+
Set if artist is to be included in layout calculations
892+
like `.Figure.tight_layout` and constrained layout. Also
893+
for ``fig.savefig(fname, bbox_inches='tight').
894+
895+
Parameters
896+
----------
897+
in_layout : bool
898+
"""
899+
self._in_layout = in_layout
900+
881901
def update(self, props):
882902
"""
883903
Update this artist's properties from the dictionary *prop*.

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,18 +4099,18 @@ def get_default_bbox_extra_artists(self):
40994099
"""
41004100
Return a default list of artists that are used for the bounding box
41014101
calculation. Artists are excluded either by not being visible
4102-
or ``artist.inbbox = False``.
4102+
or ``artist.set_in_layout(False)``.
41034103
"""
41044104
return [artist for artist in self.get_children()
4105-
if (artist.get_visible() and artist.inbbox)]
4105+
if (artist.get_visible() and artist.get_in_layout())]
41064106

41074107
def get_tightbbox(self, renderer, call_axes_locator=True,
41084108
bbox_extra_artists=None):
41094109
"""
41104110
Return the tight bounding box of the axes, including axis and their
41114111
decorators (xlabel, title, etc).
41124112
4113-
Artists that have ``artist.inbbox = False`` are not included
4113+
Artists that have ``artist.set_in_layout(False)`` are not included
41144114
in the bbox.
41154115
41164116
Parameters

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,7 @@ def waitforbuttonpress(self, timeout=-1):
19901990

19911991
def get_default_bbox_extra_artists(self):
19921992
bbox_artists = [artist for artist in self.get_children()
1993-
if (artist.get_visible() and artist.inbbox)]
1993+
if (artist.get_visible() and artist.get_in_layout())]
19941994
for ax in self.axes:
19951995
if ax.get_visible():
19961996
bbox_artists.extend(ax.get_default_bbox_extra_artists())

lib/matplotlib/tests/test_figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,12 @@ def test_tightbbox():
399399
assert np.abs(fig.get_tightbbox(renderer).x0 - 0.679) < 0.05
400400
# now exclude t from the tight bbox so now the bbox is quite a bit
401401
# smaller
402-
t.inbbox = False
402+
t.set_in_layout(False)
403403
x1Nom = 7.333
404404
assert np.abs(ax.get_tightbbox(renderer).x1 - x1Nom * fig.dpi) < 2
405405
assert np.abs(fig.get_tightbbox(renderer).x1 - x1Nom) < 0.05
406406

407-
t.inbbox = True
407+
t.set_in_layout(True)
408408
x1Nom = 7.333
409409
assert np.abs(ax.get_tightbbox(renderer).x1 - x1Nom0 * fig.dpi) < 2
410410
# test bbox_extra_artists method...

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,22 @@ def example_plot(ax, fontsize=12, nodec=False):
185185

186186
#############################################
187187
# 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
188+
# from the subplot layout, we can ``leg.set_in_layout(False)``.
189+
# Of course this can mean the legend ends up
190190
# cropped, but can be useful if the plot is subsequently called
191191
# with ``fig.savefig('outname.png', bbox_inches='tight')``. Note,
192-
# however, that the legend's inbbox status will have to be
192+
# however, that the legend's ``get_in_layout`` status will have to be
193193
# toggled again to make the saved file work:
194194

195195
fig, axs = plt.subplots(2, 2, constrained_layout=True)
196196
for ax in axs.flatten()[:-1]:
197197
ax.plot(np.arange(10))
198198
axs[1, 1].plot(np.arange(10), label='This is a plot')
199199
leg = axs[1, 1].legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
200-
leg.inbbox = False
200+
leg.set_in_layout(False)
201201
wanttoprint = False
202202
if wanttoprint:
203-
leg.inbbox = True
203+
leg.set_in_layout(True)
204204
fig.do_constrained_layout(False)
205205
fig.savefig('outname.png', bbox_inches='tight')
206206

tutorials/intermediate/tight_layout_guide.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,12 @@ def example_plot(ax, fontsize=12):
304304
# However, sometimes this is not desired (quite often when using
305305
# ``fig.savefig('outname.png', bbox_inches='tight')``). In order to
306306
# remove the legend from the bounding box calculation, we simply set its
307-
# bounding ``inbbox=False`` and the legend will be ignored.
307+
# bounding ``leg.set_in_layout(False)`` and the legend will be ignored.
308308

309309
fig, ax = plt.subplots(figsize=(4, 3))
310310
lines = ax.plot(range(10), label='B simple plot')
311311
leg = ax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',)
312-
leg.inbbox = False
312+
leg.set_in_layout(False)
313313
fig.tight_layout()
314314
plt.show()
315315

0 commit comments

Comments
 (0)
0