Description
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.
It also happens with a y-label that is just one line:
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()
)
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:
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