Python中的 Matplotlib.pyplot.suptitle()函数
Matplotlib是Python中的一个库,它是 NumPy 库的数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。
matplotlib.pyplot.suptitle()函数
matplotlib 库的 pyplot 模块中的suptitle()函数用于为图形添加标题。
Syntax: matplotlib.pyplot.suptitle(t, **kwargs)
Parameters: This function will have following parameters:
- t : The title text you want to add to your graph.
- x : The x location of the text in figure coordinates. It’s default value is 0.5.
- y : The y location of the text in figure coordinates. It’s default value is 0.98.
- horizontalalignment (ha) : {‘center’, ‘left’, right’}, The horizontal alignment of the text is relative to (x, y). It’s default value is ‘center’.
- verticalalignment (va) : {‘top’, ‘center’, ‘bottom’, ‘baseline’}, The vertical alignment of the text is relative to (x, y). It’s default value is ‘top’.
- fontsize, size : {size in points, ‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}, The font size of the text. It’s default value is ‘large’.
- fontweight, weight : {a numeric value in range 0-1000, ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’}, The font weight of the text. It’s default value is ‘normal’.
- fontproperties : None or dict, A dict of font properties. If fontproperties is given the default values for font size and weight are taken from the FontProperties defaults. rcParams[“figure.titlesize”] = ‘large’ and rcParams[“figure.titleweight”] = ‘normal’ are ignored in this case.
- **kwargs : Additional kwargs are matplotlib.text.Text properties.
Returns: The Text instance of the title.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.suptitle()函数:
示例 1:为图表添加标题,字体大小为 12。
Python3
# importing matplotlib.pyplot module
import matplotlib.pyplot as plt
# values of x and y axes
x = [6, 12, 18,
24, 30, 36,
42, 48, 54,
60]
y = [1, 4, 3,
2, 7, 6,
9, 8, 10,
5]
# plotting the graph
plt.plot(x, y)
# labelling axes
plt.xlabel('x')
plt.ylabel('y')
# adding title to the graph
# with font size 12
plt.suptitle('This is the figure title',
fontsize = 12)
# show the plot
plt.show()
Python3
# importing matplotlib.pyplot module
import matplotlib.pyplot as plt
# values of x and y axes
x = [6, 12, 18,
24, 30, 36,
42, 48, 54,
60]
y = [1, 4, 3,
2, 7, 6,
9, 8, 10,
5]
# plotting the graph
plt.plot(x, y)
# labelling axes
plt.xlabel('x')
plt.ylabel('y')
# Adding title to the graph
# with left horizontal alignment
# and font size 12.
plt.suptitle('This is the figure title',
ha = 'left',
fontsize = 12)
Python3
# importing matplotlib.pyplot module
import matplotlib.pyplot as plt
# values of x and y axes
x = [6, 12, 18,
24, 30, 36,
42, 48, 54,
60]
y = [1, 4, 3,
2, 7, 6,
9, 8, 10,
5]
# plotting the graph
plt.plot(x, y)
# labelling axes
plt.xlabel('x')
plt.ylabel('y')
# Adding title to the graph
# with extra bold font weight
# and large font size.
plt.suptitle('This is the figure title',
fontsize = 'xx-large',
weight = 'extra bold')
输出 :
示例 2:为图表添加标题,左水平对齐和字体大小为 12。
Python3
# importing matplotlib.pyplot module
import matplotlib.pyplot as plt
# values of x and y axes
x = [6, 12, 18,
24, 30, 36,
42, 48, 54,
60]
y = [1, 4, 3,
2, 7, 6,
9, 8, 10,
5]
# plotting the graph
plt.plot(x, y)
# labelling axes
plt.xlabel('x')
plt.ylabel('y')
# Adding title to the graph
# with left horizontal alignment
# and font size 12.
plt.suptitle('This is the figure title',
ha = 'left',
fontsize = 12)
输出:
示例 3:为图表添加标题,使用超粗字体粗细和大字体。
Python3
# importing matplotlib.pyplot module
import matplotlib.pyplot as plt
# values of x and y axes
x = [6, 12, 18,
24, 30, 36,
42, 48, 54,
60]
y = [1, 4, 3,
2, 7, 6,
9, 8, 10,
5]
# plotting the graph
plt.plot(x, y)
# labelling axes
plt.xlabel('x')
plt.ylabel('y')
# Adding title to the graph
# with extra bold font weight
# and large font size.
plt.suptitle('This is the figure title',
fontsize = 'xx-large',
weight = 'extra bold')
输出: