I created 3 subplots using subplot()
. Now I'd like to add titles for each subplot. Which one of title()
and suptitle()
shall I use?
我使用subplot()创建了3个子图。现在我想为每个子图添加标题。我应该使用title()和suptitle()中的哪一个?
In general, what is the difference between them? Thanks!
一般来说,它们之间有什么区别?谢谢!
1 个解决方案
#1
You can set the main figure title with fig.suptitle
and subplot's titles with ax.set_title
or by passing title
to fig.add_subplot
. For example:
您可以使用fig.suptitle设置主图标题,使用ax.set_title设置子图标题,或者将标题传递给fig.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()
(You may need to manually tweak the font sizes to get the styling you want). I think subtitles need special placement and sizing of the text. For example, see Giving graphs a subtitle in matplotlib
(您可能需要手动调整字体大小以获得所需的样式)。我认为字幕需要特殊的放置和文本大小。例如,请参阅在matplotlib中为图表提供字幕
#1
You can set the main figure title with fig.suptitle
and subplot's titles with ax.set_title
or by passing title
to fig.add_subplot
. For example:
您可以使用fig.suptitle设置主图标题,使用ax.set_title设置子图标题,或者将标题传递给fig.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()
(You may need to manually tweak the font sizes to get the styling you want). I think subtitles need special placement and sizing of the text. For example, see Giving graphs a subtitle in matplotlib
(您可能需要手动调整字体大小以获得所需的样式)。我认为字幕需要特殊的放置和文本大小。例如,请参阅在matplotlib中为图表提供字幕