0

I have the following code:

fig1 = plt.figure(figsize=(10, 10))

# Objeto
ax1 = fig1.add_subplot(3, 1, 1)
ax1.plot(xL, fL, 'k')
ax1.set_xlim(-0.04, 0.04)
ax1.set_ylim(0, 2.1)
ax1.set_title('Objeto')
ax1.set_xlabel('d (cm)')
ax1.set_ylabel('Intensidade')
ax1.grid(axis='both')

# Echo
ax2 = fig1.add_subplot(3, 1, 2)
tempo = np.arange(0, N0) * dT
CurvaT2 = exp(-tempo / T2)
ax2.plot(tempo, Ms[0, :], 'b', tempo, Ms[1, :], 'k-')
ax2.set_title('Echo')  # 'FontSize',12
ax2.set_xlabel('Tempo (ms)')
ax2.set_ylabel('Intensidade')
ax2.grid(axis='both')

# Módulo Magnetização
ax3 = fig1.add_subplot(3, 1, 3)
ax3.plot(tempo, Mod, 'k', tempo, CurvaT2, 'g--')
ax3.set_title('Módulo Magnetização')  # 'FontSize',12
ax3.set_xlabel('Tempo (ms)')
ax3.set_ylabel('Intensidade')
ax3.grid(axis='both')

Which produces the following image: enter image description here

As you can see, the title and the xlabels are mixed up. What could I do to fix that?

camposouza
  • 13
  • 3
  • 1
    Often, calling `plt.tight_layout()` at the end works for this kind of situations. If not, `plt.subplots_adjust(hspace=0.2)` or so, might work (`plt.tight_layout()` is recommended in case it gives a good result, as it would keep working after changes to the plot). – JohanC Dec 10 '21 at 22:35

1 Answers1

1

An easy and practical way to resolve this is using the method plt.tight_layout() at the end of the code. This method automatically separates in a good way the figures.

Example:

and you can increase the separation between them with the pad parameter: plt.tight_layout(pad = 2) (the default is 1.08).

More details about this lovely function:

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html