Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e60ecf

Browse files
committed
ENH have ax.get_tightbbox have a bbox around all artists
1 parent 5d33273 commit 6e60ecf

File tree

5 files changed

+26
-39
lines changed

5 files changed

+26
-39
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ a figure and adjust spacing between axes.
88

99
In Matplotlib 2.2 ``get_tightbbox`` started to include legends made on the
1010
axes, but still excluded some other artists, like text that may overspill an
11-
axes. For Matplotlib 3.0, *all* artists are not included in the bounding box.
11+
axes. For Matplotlib 3.0, *all* artists are now included in the bounding box.
1212

13-
However, it is sometimes undesirable to include an artist in the bounding box
14-
of the axes. There are two ways of dealing with this. The *most* desiable
15-
is to make the artist a child of the figure, not the axes. i.e. calling
16-
``fig.legend`` instead of ``ax.legend`` (perhaps using
17-
`~.matplotlib.Axes.get_legend_handles_labels` to gather handles and labels
18-
from the parent axes).
13+
This new default may be overridden in either of two ways:
1914

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.
15+
1) Make the artist to be excluded a child of the figure, not the axes. E.g.,
16+
call `fig.legend()`` instead of ``ax.legend()`` (perhaps using
17+
`~.matplotlib.Axes.get_legend_handles_labels` to gather handles and labels
18+
from the parent axes).
19+
2) If the artist is a child of the axes, set the artist property
20+
``artist.set_in_layout(False)``.

lib/matplotlib/artist.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,12 @@ def get_animated(self):
740740

741741
def get_in_layout(self):
742742
"""
743-
Return if artist is to be included in layout calculations
744-
like `.Figure.tight_layout` and constrained layout. Also
745-
for ``fig.savefig(fname, bbox_inches='tight')``.
743+
Return boolean flag, ``True`` if artist is included in layout
744+
calculations.
745+
746+
E.g. :doc:`/tutorials/intermediate/constrained_layout`,
747+
`.Figure.tight_layout()`, and
748+
``fig.savefig(fname, bbox_inches='tight')``.
746749
"""
747750
return self._in_layout
748751

@@ -883,9 +886,10 @@ def set_animated(self, b):
883886

884887
def set_in_layout(self, in_layout):
885888
"""
886-
Set if artist is to be included in layout calculations
887-
like `.Figure.tight_layout` and constrained layout. Also
888-
for ``fig.savefig(fname, bbox_inches='tight')```.
889+
Set if artist is to be included in layout calculations,
890+
E.g. :doc:`/tutorials/intermediate/constrained_layout`,
891+
`.Figure.tight_layout()`, and
892+
``fig.savefig(fname, bbox_inches='tight')``.
889893
890894
Parameters
891895
----------

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4110,8 +4110,10 @@ def pick(self, *args):
41104110
def get_default_bbox_extra_artists(self):
41114111
"""
41124112
Return a default list of artists that are used for the bounding box
4113-
calculation. Artists are excluded either by not being visible
4114-
or ``artist.set_in_layout(False)``.
4113+
calculation.
4114+
4115+
Artists are excluded either by not being visible or
4116+
``artist.set_in_layout(False)``.
41154117
"""
41164118
return [artist for artist in self.get_children()
41174119
if (artist.get_visible() and artist.get_in_layout())]
@@ -4146,7 +4148,7 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41464148
Returns
41474149
-------
41484150
bbox : `.BboxBase`
4149-
containing the bounding box (in figure pixel co-ordinates).
4151+
bounding box in figure pixel coordinates.
41504152
"""
41514153

41524154
bb = []

lib/matplotlib/figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,7 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None):
20172017
"""
20182018
Return a (tight) bounding box of the figure in inches.
20192019
2020-
Artists that have ``artist.inbbox = False`` are not included
2020+
Artists that have ``artist.set_in_layout(False)`` are not included
20212021
in the bbox.
20222022
20232023
Parameters
@@ -2104,7 +2104,7 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
21042104
21052105
To exclude an artist on the axes from the bounding box calculation
21062106
that determines the subplot parameters (i.e. legend, or annotation),
2107-
then set `a.inbbox=False` for that artist.
2107+
then set `a.set_in_layout(False)` for that artist.
21082108
21092109
Parameters
21102110
----------

lib/matplotlib/legend.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -992,25 +992,7 @@ def get_tightbbox(self, renderer):
992992
993993
Returns
994994
-------
995-
`.BboxBase` : containing the bounding box (in figure-relative
996-
co-ordinates).
997-
"""
998-
return self._legend_box.get_window_extent(renderer)
999-
1000-
def get_tightbbox(self, renderer):
1001-
"""
1002-
Like `.Legend.get_window_extent`, but uses the box for the legend.
1003-
1004-
Parameters
1005-
----------
1006-
renderer : `.RendererBase` instance
1007-
renderer that will be used to draw the figures (i.e.
1008-
``fig.canvas.get_renderer()``)
1009-
1010-
Returns
1011-
-------
1012-
`.BboxBase` : containing the bounding box (in figure-relative
1013-
co-ordinates).
995+
`.BboxBase` : containing the bounding box in figure pixel co-ordinates.
1014996
"""
1015997
return self._legend_box.get_window_extent(renderer)
1016998

0 commit comments

Comments
 (0)