From 464330c4da1e24b2247aa4e4f8ef57d203aceb5c Mon Sep 17 00:00:00 2001 From: buddy0452004 Date: Sat, 25 Apr 2026 19:52:22 +0530 Subject: [PATCH] FIX: Include axis labels in Axes3D.get_tightbbox() to prevent clipping Previously, the zlabel (and x/y labels) were excluded from the tightbbox when for_layout_only=True, causing savefig(bbox_inches='tight') to clip them in the saved output. Harmonize 3D axis label bbox handling with 2D axes behavior: include the label bbox but collapse it in the layout direction when for_layout_only=True. Fixes #28117 --- lib/mpl_toolkits/mplot3d/axis3d.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/mpl_toolkits/mplot3d/axis3d.py b/lib/mpl_toolkits/mplot3d/axis3d.py index 0ac2e50b1a1a..a5c87c9d3cd4 100644 --- a/lib/mpl_toolkits/mplot3d/axis3d.py +++ b/lib/mpl_toolkits/mplot3d/axis3d.py @@ -722,9 +722,22 @@ def get_tightbbox(self, renderer=None, *, for_layout_only=False): other.append(self.offsetText.get_window_extent(renderer)) if self.line.get_visible(): other.append(self.line.get_window_extent(renderer)) - if (self.label.get_visible() and not for_layout_only and - self.label.get_text()): - other.append(self.label.get_window_extent(renderer)) + if self.label.get_visible() and self.label.get_text(): + bb = self.label.get_window_extent(renderer) + if for_layout_only: + if self.axis_name == "z": + if bb.height > 0: + bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5 + bb.y1 = bb.y0 + 1.0 + elif self.axis_name == "x": + if bb.width > 0: + bb.x0 = (bb.x0 + bb.x1) / 2 - 0.5 + bb.x1 = bb.x0 + 1.0 + elif self.axis_name == "y": + if bb.height > 0: + bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5 + bb.y1 = bb.y0 + 1.0 + other.append(bb) return mtransforms.Bbox.union([*bb_1, *bb_2, *other])