在matplotlib
中常用的标题主要三种:窗口标题、图像标题和子图标题。
先通过三个案例简要说明这三类标题的实现。
窗口标题、图像标题,子图标题(仅1个子图)
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = 'SimHei' fig = plt.figure() plt.plot([1, 2]) # 设置图像标题 plt.suptitle("这是图像标题") # 设置子图标题 plt.title("这是子图标题") # 获取默认窗口标题 current_title = fig.canvas.manager.window.windowTitle() print("默认窗口:",current_title) # 设置窗口标题方式一 fig.canvas.set_window_title("这是窗口标题") # 设置窗口标题方式二 fig.canvas.manager.window.setWindowTitle("这是窗口标题") plt.show()
窗口标题、图像标题、子图标题(多子图)
使用subplot
函数实现子图
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = 'SimHei' fig = plt.figure() plt.subplot(1, 2, 1) plt.plot([1,2,3,4], [1,4,9,16], "go") # 设置子图1标题 plt.title("子图1标题") plt.subplot(122) plt.plot([1,2,3,4], [1,4,9,16], "r^") # r^ 表示 红色(red)三角 # 设置子图2标题 plt.title("子图2标题") # 设置图像标题 plt.suptitle("图像标题") # 设置窗口标题 #fig.canvas.set_window_title("这是窗口标题") fig.canvas.manager.window.setWindowTitle("这是窗口标题") plt.show()
使用subplots函数subplots实现子图
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = 'SimHei' fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(6,6)) ax[0].plot([1,2,3,4], [1,4,9,16], "go") # 设置子图1标题 ax[0].set_title("子图1标题") ax[1].plot([1,2,3,4], [1,4,9,16], "r^") # 设置子图2标题 ax[1].set_title("子图2标题") # 设置图像标题 plt.suptitle("图像标题") # 设置窗口标题 fig.canvas.manager.window.setWindowTitle("这是窗口标题") plt.show()
原理分析
通过前面三个案例可知:
窗口标题
设置窗口标题可以用两种方法:
一种是调用figure.canvas
对象的set_window_title
方法,一种是figure.canvas.manager.window
对象的setWindowTitle
方法。通过下面源码可知,这两种方法其实是等价的本文来源gao@!dai!ma.com搞$$代^@码网*。
因此在日常实现过程中,关键是获取当前图像对象(figure
),即案例中的fig
。该方法只有一个参数,类型为字符串。
可以通过
通过figure.canvas.manager.window
对象的windowTitle
方法可以获取窗口标题。
class FigureManagerQT(FigureManagerBase): def set_window_title(self, title): self.window.setWindowTitle(title)
图像标题
调用plt.suptitle
函数即可。根据源码可知,plt.suptitle
函数其实是调用了当前figure
对象的suptitle
方法。
suptitle
函数参数
def suptitle(self, t, **kwargs): """ Add a centered title to the figure. Parameters ---------- t : str The title text. x : float, default 0.5 The x location of the text in figure coordinates. y : float, default 0.98 The y location of the text in figure coordinates. horizontalalignment, ha : {'center', 'left', right'}, default: 'center' The horizontal alignment of the text relative to (*x*, *y*). verticalalignment, va : {'top', 'center', 'bottom', 'baseline'}, \ default: 'top' The vertical alignment of the text relative to (*x*, *y*). fontsize, size : default: :rc:`figure.titlesize` The font size of the text. See `.Text.set_size` for possible values. fontweight, weight : default: :rc:`figure.titleweight` The font weight of the text. See `.Text.set_weight` for possible values. Returns ------- text The `.Text` instance of the title. Other Parameters ---------------- fontproperties : None or dict, optional A dict of font properties. If *fontproperties* is given the default values for font size and weight are taken from the `.FontProperties` defaults. :rc:`figure.titlesize` and :rc:`figure.titleweight` are ignored in this case. **kwargs Additional kwargs are `matplotlib.text.Text` properties. Examples -------- >>> fig.suptitle('This is the figure title', fontsize=12) """