在matplotlib中,title()和suptitle()之间有什么区别?

pya*_*yan 7 python matplotlib

我使用创建了3个子图subplot().现在我想为每个子图添加标题.其中一个title()suptitle()我应该使用?

一般来说,它们之间有什么区别?谢谢!

xnx*_*xnx 8

您可以设置与主图标题fig.suptitle和次要情节与职称ax.set_title或传递titlefig.add_subplot.例如:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-np.pi, np.pi, 0.01)

fig = plt.figure()
fig.suptitle('Main figure title')

ax1 = fig.add_subplot(311, title='Subplot 1 title')
ax1.plot(x, np.sin(x))

ax2 = fig.add_subplot(312)
ax2.set_title('Subplot 2 title')
ax2.plot(x, np.cos(x))

ax3 = fig.add_subplot(313)
ax3.set_title('Subplot 3 title')
ax3.plot(x, np.tan(x))

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

(您可能需要手动调整字体大小以获得所需的样式).我认为字幕需要特殊的放置和文本大小.例如,请参阅在matplotlib中为图表提供字幕

  • 谢谢你的具体例子。我将 `suptitle()` 误读为 `subtitle()`。现在说得通了。 (2认同)