1

I am implementing an algorithme with Python and I would like to plot 2 subplots.

This is what I obtain:

enter image description here

I would like to know if there is a way to automaticaly adjust the space between the two plots to see the title & the xlabel?

Thanks

I don't know how to do it.

  • Try `plt.tight_layout()` just before `plt.show()` – Corralien Feb 13 '23 at 08:59
  • In this case you might also want to use `sharex=True` (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html). – moooeeeep Feb 13 '23 at 09:15
  • 1
    Does this answer your question? [Improve subplot size/spacing with many subplots](https://stackoverflow.com/questions/6541123/improve-subplot-size-spacing-with-many-subplots) – Jody Klymak Feb 13 '23 at 17:42
  • See in particular the comment about constrained_layout (tight layout is mildly discouraged) https://stackoverflow.com/a/51657238/3394386 – Jody Klymak Feb 13 '23 at 17:43

1 Answers1

1

You have to use plt.tight_layout:

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
ax1.plot(arr.real)
ax1.set_title('Real part at the output of the filter')
ax2.plot(arr.imag)
ax2.set_title('Imaginary part at the output of the filter')
plt.tight_layout()
plt.show()

With plt.tight_layout:

enter image description here

Without plt.tight_layout:

enter image description here

Corralien
  • 109,409
  • 8
  • 28
  • 52