Skip to content

Commit fdf1bb5

Browse files
committed
Merge pull request #4477 from has2k1/tight_layout-for-offsetboxes
ENH: OffsetBoxes now considered by tight_layout
2 parents a951b75 + 5fb1b9d commit fdf1bb5

File tree

10 files changed

+3170
-2
lines changed

10 files changed

+3170
-2
lines changed

doc/users/whats_new/offsetbox.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ text is always within the bounds, for this nothing has changed.
99
However, `DrawingArea` acts as a parent for zero or more `Artists` that
1010
draw on it and may do so beyond the bounds. Now child `Artists` can be
1111
clipped to the bounds of the `DrawingArea`.
12+
13+
14+
OffsetBoxes now considered by tight_layout
15+
``````````````````````````````````````````
16+
17+
When :method:`pyplot.tight_layout()` or :method:`Figure.tight_layout()`
18+
or :method:`GridSpec.tight_layout()` is called, `OffsetBoxes` that are
19+
anchored outside the axes will not get chopped out. The `OffsetBoxes` will
20+
also not get overlapped by other axes in case of multiple subplots.

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import matplotlib.font_manager as font_manager
3030
import matplotlib.text as mtext
3131
import matplotlib.image as mimage
32+
from matplotlib.offsetbox import OffsetBox
3233
from matplotlib.artist import allow_rasterization
3334
from matplotlib.cbook import iterable
3435

@@ -3306,6 +3307,10 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
33063307
if bb_yaxis:
33073308
bb.append(bb_yaxis)
33083309

3310+
for child in self.get_children():
3311+
if isinstance(child, OffsetBox) and child.get_visible():
3312+
bb.append(child.get_window_extent(renderer))
3313+
33093314
_bbox = mtransforms.Bbox.union(
33103315
[b for b in bb if b.width != 0 or b.height != 0])
33113316

0 commit comments

Comments
 (0)