Skip to content

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

Closed

Description

@Saltsmart

Bug summary

The zlabel on 3D axes will be cut when using '%matplotlib inline' in Jupyter, but it's no problem to use non-interactive backends.

Code for reproduction

from matplotlib import pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

ax.set_zlabel("Z Label")

plt.show()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

No response

Matplotlib Version

3.8.0

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.11.5

Jupyter version

6.5.4

Installation

conda

Activity

  1. QuLogic commented on Jul 8, 2024

    @QuLogic
    Member

    This bisects to 5c75807.

  2. QuLogic commented on Jul 9, 2024

    @QuLogic
    Member

    I think the issue is that previously, we passed for_layout_only through from the caller for Axes and for Axes3D.

    But the new helper always uses for_layout_only=True.

    For whatever reason, this produces a different result for the Z label only:

    x:
    for_layout_only=False: Bbox(x0=153.64739462589117, y0=40.403546055233704, x1=378.7212068957853, y1=143.93151525468753)
    for_layout_only=True : Bbox(x0=153.64739462589117, y0=40.403546055233704, x1=378.7212068957853, y1=143.93151525468753)
    
    y:
    for_layout_only=False: Bbox(x0=378.7212068957853, y0=45.87339284336335, x1=514.5305653397124, y1=189.3128247336971)
    for_layout_only=True : Bbox(x0=378.7212068957853, y0=45.87339284336335, x1=514.5305653397124, y1=189.3128247336971)
    
    z:
    for_layout_only=False: Bbox(x0=489.01444521607, y0=181.84939294449146, x1=556.6806461388192, y1=342.06356412937953)
    for_layout_only=True : Bbox(x0=489.01444521607, y0=181.84939294449146, x1=533.2899776013838, y1=342.06356412937953)
    
  3. QuLogic commented on Jul 9, 2024

    @QuLogic
    Member

    So something like this fixes it, but I'm not sure it's right:

    diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py
    index d5b8631e95..101880c027 100644
    --- a/lib/matplotlib/artist.py
    +++ b/lib/matplotlib/artist.py
    @@ -1405,7 +1405,7 @@ def _get_tightbbox_for_layout_only(obj, *args, **kwargs):
         when encountering third-party subclasses that do not support it.
         """
         try:
    -        return obj.get_tightbbox(*args, **{**kwargs, "for_layout_only": True})
    +        return obj.get_tightbbox(*args, **{"for_layout_only": True, **kwargs})
         except TypeError:
             return obj.get_tightbbox(*args, **kwargs)
     
    diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py
    index 12f3682ae5..5895b22353 100644
    --- a/lib/mpl_toolkits/mplot3d/axes3d.py
    +++ b/lib/mpl_toolkits/mplot3d/axes3d.py
    @@ -3769,7 +3769,7 @@ class Axes3D(Axes):
                 for axis in self._axis_map.values():
                     if axis.get_visible():
                         axis_bb = martist._get_tightbbox_for_layout_only(
    -                        axis, renderer)
    +                        axis, renderer, for_layout_only=for_layout_only)
                         if axis_bb:
                             batch.append(axis_bb)
             return mtransforms.Bbox.union(batch)

    i.e., I don't know why this only affects the Z label, and there's probably a different bug with it that's the real cause. And additionally, as noted above, this changed for regular Axes without breakage, nor needing to revert it to fix this bug, so there's something specifically fishy about for_layout_only=True for the Z label only.

  4. WeatherGod commented on Jul 9, 2024

    @WeatherGod
    Member
  5. QuLogic commented on Jul 9, 2024

    @QuLogic
    Member

    It looks like for X/Y, we flatten the label bbox in the long direction:

    if self.label.get_visible():
    bb = self.label.get_window_extent(renderer)
    # for constrained/tight_layout, we want to ignore the label's
    # width/height because the adjustments they make can't be improved.
    # this code collapses the relevant direction
    if for_layout_only:
    if self.axis_name == "x" and bb.width > 0:
    bb.x0 = (bb.x0 + bb.x1) / 2 - 0.5
    bb.x1 = bb.x0 + 1.0
    if self.axis_name == "y" and bb.height > 0:
    bb.y0 = (bb.y0 + bb.y1) / 2 - 0.5
    bb.y1 = bb.y0 + 1.0
    bboxes.append(bb)

    but for Z, we ignore it entirely:
    if (self.label.get_visible() and not for_layout_only and
    self.label.get_text()):
    other.append(self.label.get_window_extent(renderer))

    We should probably harmonize that, though the question will be whether we can do so for all rotations and/or vertical axis settings.

  6. chiavez commented on Mar 26, 2026

    @chiavez

    Same problem here, i had to manually add the label by using:

     ax.text2D(
            -0.05, 0.5, z_label, transform=ax.transAxes,
            fontsize=plt.rcParams["axes.labelsize"],
            rotation=90, va="center", ha="center",
        )

    Generally, the bounding boxes for 3D plots are buggy. There is either too much white space (#19519) or too little.

  7. added a commit that references this issue on Apr 25, 2026
    464330c
  8. added a commit that references this issue on Apr 26, 2026
    c62a06f
  9. added a commit that references this issue on Apr 27, 2026
    48ee5b8
  10. rcomer commented on Apr 28, 2026

    @rcomer
    Member

    I actually think we also have a problem for 2D

    import matplotlib.pyplot as plt
    
    plt.plot([0, 1])
    plt.xlabel('loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong')
    plt.savefig('test.png', bbox_inches='tight')
    Image

    Shouldn't the figure expand to fit the label? [Edit: confirmed that it did in v3.5]

  11. rcomer commented on Apr 28, 2026

    @rcomer
    Member

    This works, but I'm not sure it's any better than explicitly handling the for_layout_only=False case in Axes[3D].get_tightbbox, as #31571 is doing for 3D.

    diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py
    index 88e38634b5..12fcb58950 100644
    --- a/lib/matplotlib/artist.py
    +++ b/lib/matplotlib/artist.py
    @@ -1475,14 +1475,14 @@ Supported properties are
         mouseover = property(get_mouseover, set_mouseover)  # backcompat.
     
    --- a/lib/matplotlib/artist.py
    +++ b/lib/matplotlib/artist.py
    @@ -1475,14 +1475,14 @@ Supported properties are
         mouseover = property(get_mouseover, set_mouseover)  # backcompat.
     
     
    -def _get_tightbbox_for_layout_only(obj, *args, **kwargs):
    +def _get_tightbbox_for_layout_only(obj, *args, for_layout_only=True, **kwargs):
         """
         Matplotlib's `.Axes.get_tightbbox` and `.Axis.get_tightbbox` support a
         *for_layout_only* kwarg; this helper tries to use the kwarg but skips it
         when encountering third-party subclasses that do not support it.
         """
         try:
    -        return obj.get_tightbbox(*args, **{**kwargs, "for_layout_only": True})
    +        return obj.get_tightbbox(*args, for_layout_only=for_layout_only, **kwargs)
         except TypeError:
             return obj.get_tightbbox(*args, **kwargs)
     
    diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py
    index ee933ea138..aac92863f3 100644
    --- a/lib/matplotlib/axes/_base.py
    +++ b/lib/matplotlib/axes/_base.py
    @@ -4705,7 +4705,8 @@ class _AxesBase(martist.Artist):
     
             for axis in self._axis_map.values():
                 if self.axison and axis.get_visible():
    -                ba = martist._get_tightbbox_for_layout_only(axis, renderer)
    +                ba = martist._get_tightbbox_for_layout_only(
    +                    axis, renderer, for_layout_only=for_layout_only)
                     if ba:
                         bb.append(ba)
             self._update_title_position(renderer)
    diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py
    index 45f9319355..ed1588960e 100644
    --- a/lib/mpl_toolkits/mplot3d/axes3d.py
    +++ b/lib/mpl_toolkits/mplot3d/axes3d.py
    @@ -4165,7 +4165,7 @@ class Axes3D(Axes):
                 for axis in self._axis_map.values():
                     if axis.get_visible():
                         axis_bb = martist._get_tightbbox_for_layout_only(
    -                        axis, renderer)
    +                        axis, renderer, for_layout_only=for_layout_only)
                         if axis_bb:
                             batch.append(axis_bb)
             return mtransforms.Bbox.union(batch)
    
  12. added 2 commits that reference this issue on Apr 28, 2026
    1f40cca
    2ad5d7e
  13. added this to the v3.11.1 milestone on Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions