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

Skip to content

Commit fc04c62

Browse files
committed
include overspilling axes legends in ax.get_tightbbox
Added whats-new entry
1 parent 5eb185f commit fc04c62

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Axes legends now included in tight_bbox
2+
---------------------------------------
3+
4+
Legends created via ``ax.legend()`` can sometimes overspill the limits of
5+
the axis. Tools like ``fig.tight_layout()`` and
6+
``fig.savefig(bbox_inches='tight')`` would clip these legends. A change
7+
was made to include them in the ``tight`` calculations.

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import matplotlib.image as mimage
3434
from matplotlib.offsetbox import OffsetBox
3535
from matplotlib.artist import allow_rasterization
36+
from matplotlib.legend import Legend
3637

3738
from matplotlib.rcsetup import cycler
3839
from matplotlib.rcsetup import validate_axisbelow
@@ -3969,6 +3970,8 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
39693970
for child in self.get_children():
39703971
if isinstance(child, OffsetBox) and child.get_visible():
39713972
bb.append(child.get_window_extent(renderer))
3973+
elif isinstance(child, Legend) and child.get_visible():
3974+
bb.append(child._legend_box.get_window_extent(renderer))
39723975

39733976
_bbox = mtransforms.Bbox.union(
39743977
[b for b in bb if b.width != 0 or b.height != 0])

0 commit comments

Comments
 (0)