1

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!

Dianne
  • 73
  • 1
  • 8
  • 1
    I do not see the behavior you describe. Can you include your Python version and matplotlib version? Via `python -V` at commandline, and `import matplotlib; print(matplotlib.__version__)` – Basil Jan 28 '22 at 19:04
  • 1
    Same here. Not reproducible with matplotlib 3.5.1., Python 3.8, Win10 and Ubuntu 20.04. – Mr. T Jan 28 '22 at 21:24
  • 1
    For reference, my environment is Win 10, Python 3.9.6, matplotlib 3.5.1 with the PySide 6 backend. Perhaps the latest matplotlib 3.5.x is the common denominator here? – Basil Jan 29 '22 at 01:24
  • Constrained layout works with subplots though it is preferable to create them all at once. Most users should not need to use gridspecs directly. For suptitle I’m. It sure why you are seeing the described behaviour but manual placement can lead to constrained_layout leaving you to your own devices. However the algorithm has been substantially updated for 3.5 and perhaps this has been fixed – Jody Klymak Jan 29 '22 at 08:38
  • Dianne, can you write your addition as an answer and accept it so the question will not appear as "unanswered"? – Mr. T Jan 30 '22 at 11:55

1 Answers1

1

The problem occurred in Matplotlib 3.4, updating to 3.5 fixed the issue

Dianne
  • 73
  • 1
  • 8