From fc04c6273f84d043b682e435a04ba3012bbe2343 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 6 Sep 2017 17:26:04 -0700 Subject: [PATCH] include overspilling axes legends in ax.get_tightbbox Added whats-new entry --- .../next_whats_new/2017-10-19_axes_legend_in_tightbbox.rst | 7 +++++++ lib/matplotlib/axes/_base.py | 3 +++ 2 files changed, 10 insertions(+) create mode 100644 doc/users/next_whats_new/2017-10-19_axes_legend_in_tightbbox.rst diff --git a/doc/users/next_whats_new/2017-10-19_axes_legend_in_tightbbox.rst b/doc/users/next_whats_new/2017-10-19_axes_legend_in_tightbbox.rst new file mode 100644 index 000000000000..3544fa3fe08a --- /dev/null +++ b/doc/users/next_whats_new/2017-10-19_axes_legend_in_tightbbox.rst @@ -0,0 +1,7 @@ +Axes legends now included in tight_bbox +--------------------------------------- + +Legends created via ``ax.legend()`` can sometimes overspill the limits of +the axis. Tools like ``fig.tight_layout()`` and +``fig.savefig(bbox_inches='tight')`` would clip these legends. A change +was made to include them in the ``tight`` calculations. diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 79db8f9f4f1a..146f8c7596a8 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -33,6 +33,7 @@ import matplotlib.image as mimage from matplotlib.offsetbox import OffsetBox from matplotlib.artist import allow_rasterization +from matplotlib.legend import Legend from matplotlib.rcsetup import cycler from matplotlib.rcsetup import validate_axisbelow @@ -3969,6 +3970,8 @@ def get_tightbbox(self, renderer, call_axes_locator=True): for child in self.get_children(): if isinstance(child, OffsetBox) and child.get_visible(): bb.append(child.get_window_extent(renderer)) + elif isinstance(child, Legend) and child.get_visible(): + bb.append(child._legend_box.get_window_extent(renderer)) _bbox = mtransforms.Bbox.union( [b for b in bb if b.width != 0 or b.height != 0])