【问题标题】:Matplotlib title and labels are mixed up [duplicate]Matplotlib标题和标签混淆了[重复]
【发布时间】:2022-01-15 12:28:42
【问题描述】:

我有以下代码:

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')

生成以下图像:

如您所见,标题和 xlabels 混淆了。我能做些什么来解决这个问题?

【问题讨论】:

  • 通常,最后调用plt.tight_layout() 适用于这种情况。如果没有,plt.subplots_adjust(hspace=0.2) 左右可能会起作用(建议使用plt.tight_layout(),以防它产生良好的结果,因为它会在更改情节后继续工作)。

标签: python matplotlib


【解决方案1】:

解决此问题的一种简单实用的方法是在代码末尾使用方法plt.tight_layout()。这种方法可以很好地自动分离数字。

Example:

您可以使用pad 参数增加它们之间的间隔:plt.tight_layout(pad = 2)(默认为 1.08)。

更多关于这个可爱功能的细节:

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

【讨论】:

  • 这太棒了!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2023-04-04
  • 2021-01-11
  • 2017-01-23
  • 2012-11-12
  • 2020-01-22
相关资源
最近更新 更多