Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bug]: constrained layout clips y-label above y-axis #27877

Copy link
Copy link
Open
@pank

Description

@pank
Issue body actions

Bug summary

When placing the y-label above the y-axis/spine, it gets clipped with constrained layout. It works OK with tight layout and on the left in constrained layout.

Code for reproduction

import matplotlib.pyplot as plt
x = range(10)
lab = "foo bar baz"

# with extra space it works, but the second plot with two lines gets
# clipped.
fig0, axes0 = plt.subplots(1, 2, constrained_layout=True)
[ax.plot(x, x) for ax in axes0]
[ax.set_ylabel("\n".join([lab]*(i+1)),
               y=1.0,
               rotation="horizontal",
               horizontalalignment="left",
               multialignment="left",)
 for i, ax in enumerate(axes0)]

# single plot with one line also gets clipped
fig1, ax1 = plt.subplots(constrained_layout=True)
ax1.plot(x, x)
ax1.set_ylabel(lab,
               y=1.0,
               rotation="horizontal",
               horizontalalignment="left",
               multialignment="left",)

# For reference it works fine when placing the ylab on the 'side'
# as a conventional matplotlib plot
fig2, ax2 = plt.subplots(constrained_layout=True)
ax2.plot(x, x)
ax2.set_ylabel(f"{lab}\n{lab}",
               rotation="horizontal",
               horizontalalignment="right",
               multialignment="right",)

for i, fig in enumerate((fig0, fig1, fig2)):
    fig.savefig(f"fig{i}.png")

Actual outcome

The y-label gets clipped. The second plot should have a two line long y label.
fig0

It also happens with a y-label that is just one line:
fig1

Expected outcome

I would expect the y label to be included, even when putting it above the axis. Example with tight layout (fig1.tight_layout())

fig1_tight

Additional information

I think this is a bug(?).
I have tried various vertical alignments, and it doesn't make much of a difference. I also tried to follow the legend set_in_layout-example in the constrained layout guide without much luck.

A temporary fix is to set the title to ' ' or increasing the constrained layout padding, I guess.

Note that constrained layout works as expected with long y-labels on the left:

fig2

Operating system

Windows

Matplotlib Version

3.8.0

Matplotlib Backend

module://matplotlib_inline.backend_inline, but present in saved figures (savefig)

Python version

3.12.1

Jupyter version

Jupyter console 6.6.3

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: geometry managerLayoutEngine, Constrained layout, Tight layoutLayoutEngine, Constrained layout, Tight layout

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.