'Why does the title overlap with the plot when specifying x using constrained_layout?

Solved: This problem occurred with matplotlib 3.4, updating to 3.5 fixed the issue.

I am plotting multiple subplots in a graph, which all have titles, labels and subplot titles. To keep everything visible and the right size, I am using constrained_layout. I would like to add a title that is aligned to the left. However, when I specify the x position (even as 0.5 which is the default), the title overlaps with the graph.

My plots are much more complex, but this already shows my issue:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10, 5), constrained_layout=True)
gs = fig.add_gridspec(1,1)
ax1 = fig.add_subplot(gs[0,0])
fig.suptitle('Title', ha='left')

Title does not overlaps

Only changing the last line of code:

fig.suptitle('Title with x-position', x=0.5, ha='left')

Title overlaps

I was first using tight layout, but switched to constrained_layout because tight_layout did not keep the specified size of the figure when exporting it. I have also switched from subplots to gridspec because I read that constrained_layout does not support subplots. I know I can add extra space with fig.set_constrained_layout_pads(h_pad=0.3), but this also adds space below the plots, which I would like to avoid.

Hopefully someone can tell me why this happens and how I can get a title aligned to the left that does not overlap with the plot!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source