Skip to content

FIX: Include axis labels in Axes3D.get_tightbbox() to prevent clipping#31572

Open
buddy0452004 wants to merge 1 commit intomatplotlib:mainfrom
buddy0452004:fix-3d-zlabel-tightbbox
Open

FIX: Include axis labels in Axes3D.get_tightbbox() to prevent clipping#31572
buddy0452004 wants to merge 1 commit intomatplotlib:mainfrom
buddy0452004:fix-3d-zlabel-tightbbox

Conversation

@buddy0452004
Copy link
Copy Markdown
Contributor

@buddy0452004 buddy0452004 commented Apr 25, 2026

Fixes #28117

Previously, axis labels on 3D axes were excluded entirely from the tightbbox when for_layout_only=True. This caused savefig(bbox_inches='tight') to clip these labels in the saved output since bbox_inches='tight' uses get_tightbbox() internally. The inline Jupyter backend (%matplotlib inline) also triggers this path, which is the original report in #28117.

Root cause: In axis3d.py, the label was guarded by not for_layout_only:

if (self.label.get_visible() and not for_layout_only and
        self.label.get_text()):
    other.append(self.label.get_window_extent(renderer))

Fix: Harmonize with 2D axis behavior (axis.py lines 1368–1380) always include the label bbox, but collapse it in the irrelevant direction when for_layout_only=True.

Minimum reproducible example:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4, 3))
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.plot([0, 1], [0, 1], [0, 1])
ax.set_zlabel("Z axis label")
ax.set_xlabel("X axis label")
fig.savefig("out.png", bbox_inches='tight')

Before fix: zlabel and xlabel are clipped in the saved image. After fix: all labels are fully visible.

AI Disclosure

None.

PR checklist

@github-actions github-actions Bot added topic: mplot3d Documentation: examples files in galleries/examples labels Apr 25, 2026
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 matplotlib#28117
@buddy0452004 buddy0452004 force-pushed the fix-3d-zlabel-tightbbox branch from c0bf5d5 to 464330c Compare April 25, 2026 14:36
@github-actions github-actions Bot removed the Documentation: examples files in galleries/examples label Apr 25, 2026
@buddy0452004
Copy link
Copy Markdown
Contributor Author

The image comparison failure in test_axes3d_primary_views is expected the fix correctly includes axis labels in the tightbbox, which slightly changes the layout when tight_layout() is used with 3D axes that have labels. I will update the baseline image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The zlabel on 3D axes will be cut when using '%matplotlib inline' in Jupyter

1 participant