如何为 Matplotlib 中的所有子图设置单个主标题?
先决条件: Matplotlib
matplotlib 库中的标题描述了绘制图形的主要主题。使用 title() 方法很容易为一个图设置标题。通过使用此函数,只能设置单个标题图,但不能为所有子图设置单个标题。因此,要为所有子图设置单个主标题,请使用 suptitle() 方法。
Syntax: suptitle(self, t, **kwargs)
Parameters: This method accept the following parameters that are discussed below:
- t : This parameter is the title text.
- x: This parameter is the x location of the text in figure coordinates.
- y: This parameter is the y location of the text in figure coordinates.
- horizontalalignment, ha : This parameter is the horizontal alignment of the text relative to (x, y).
- verticalalignment, va : This parameter is the vertical alignment of the text relative to (x, y).
- fontsize, size : This parameter is the font size of the text.
- fontweight, weight : This parameter is the font weight of the text.
Returns: This method returns the Text instance of the title.
方法
- 导入模块
- 创建要绘制的数据
- 使用 suptitle() 设置图形标题
- 绘制图形
- 显示图
使用给出的方法实现如下:
示例 1:
Python3
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
x1 = [1, 2, 3, 4, 5, 6]
y1 = [45, 34, 30, 45, 50, 38]
y2 = [36, 28, 30, 40, 38, 48]
labels = ["student 1", "student 2"]
fig.suptitle(' Student marks in different subjects ', fontsize=30)
# Creating the sub-plots.
l1 = ax1.plot(x1, y1, 'o-', color='g')
l2 = ax2.plot(x1, y2, 'o-')
fig.legend([l1, l2], labels=labels,
loc="upper right")
plt.subplots_adjust(right=0.9)
plt.show()
Python3
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(2, 2)
fig.suptitle('** Main Title for all sub plots **', fontsize=20)
plt.style.use('seaborn')
labels = ['first line', 'second line',
'third line', 'fourth line', 'fifth line']
ax[0, 0].plot([1, 2, 3, 4, 5], [9, 3, 5, 7, 9], '-.', color='g')
ax[0, 0].set_title('first subplot')
ax[0, 0].set_yticks(np.arange(1, 10))
ax[0, 1].plot([1, 2, 3, 4, 5, 6], [1, 4, 9, 7, 9, 8], '-*', color='m')
ax[0, 1].set_title('second subplot')
ax[0, 1].set_yticks(np.arange(1, 10))
ax[1, 0].plot([1, 2, 3, 4, 5], [8, 5, 2, 3, 3], '-v', color='r')
ax[1, 0].set_title('third subplot')
ax[1, 0].set_yticks(np.arange(1, 9))
ax[1, 1].plot([1, 2, 3, 4, 5, 6], [1, 3, 5, 7, 9, 6], 'o-', color='b')
ax[1, 1].plot([1, 2, 3, 4, 5, 6, 7, 7], [
7, 8, 6, 5, 2, 2, 4, 6], 'o-', color='g')
ax[1, 1].set_title('fourth subplot')
ax[1, 1].set_yticks(np.arange(1, 10))
fig.tight_layout()
fig.legend(ax, labels=labels, loc="upper right", borderaxespad=0.1)
fig.subplots_adjust(top=0.85)
plt.show()
输出:
示例 2:
蟒蛇3
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(2, 2)
fig.suptitle('** Main Title for all sub plots **', fontsize=20)
plt.style.use('seaborn')
labels = ['first line', 'second line',
'third line', 'fourth line', 'fifth line']
ax[0, 0].plot([1, 2, 3, 4, 5], [9, 3, 5, 7, 9], '-.', color='g')
ax[0, 0].set_title('first subplot')
ax[0, 0].set_yticks(np.arange(1, 10))
ax[0, 1].plot([1, 2, 3, 4, 5, 6], [1, 4, 9, 7, 9, 8], '-*', color='m')
ax[0, 1].set_title('second subplot')
ax[0, 1].set_yticks(np.arange(1, 10))
ax[1, 0].plot([1, 2, 3, 4, 5], [8, 5, 2, 3, 3], '-v', color='r')
ax[1, 0].set_title('third subplot')
ax[1, 0].set_yticks(np.arange(1, 9))
ax[1, 1].plot([1, 2, 3, 4, 5, 6], [1, 3, 5, 7, 9, 6], 'o-', color='b')
ax[1, 1].plot([1, 2, 3, 4, 5, 6, 7, 7], [
7, 8, 6, 5, 2, 2, 4, 6], 'o-', color='g')
ax[1, 1].set_title('fourth subplot')
ax[1, 1].set_yticks(np.arange(1, 10))
fig.tight_layout()
fig.legend(ax, labels=labels, loc="upper right", borderaxespad=0.1)
fig.subplots_adjust(top=0.85)
plt.show()
输出: