首页 > 编程技术 > python

matplotlib之pyplot模块之标题(title()和suptitle())

发布时间:2021-2-23 00:00

matplotlib 源码解析标题实现(窗口标题,标题,子图标题不同之间的差异)添加链接描述简单比较了matplotlib中的标题。

使用title()设置子图标题

title()同时在子图中显示中间、左侧、右侧3个标题
函数签名为matplotlib.pyplot.title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs)
参数作用及取值如下:

{'fontsize': rcParams['axes.titlesize'],
 'fontweight': rcParams['axes.titleweight'],
 'color': rcParams['axes.titlecolor'],
 'verticalalignment': 'baseline',
 'horizontalalignment': loc}

返回值为Text对象。

title()相关rcParams为:

#axes.titlelocation: center # alignment of the title: {left, right, center}
#axes.titlesize:   large  # fontsize of the axes title
#axes.titleweight:  normal # font weight of title
#axes.titlecolor:  auto  # color of the axes title, auto falls back to
               # text.color as default value
#axes.titley:    None  # position title (axes relative units). None implies auto
#axes.titlepad:   6.0   # pad between axes and title in points

底层相关方法为:
Axes.set_title(self, label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs)
Axes.get_title(self, loc='center')注意返回指定位置的标题文本。

案例

同时设置3个子图标题。

import matplotlib.pyplot as plt

# 注意,子图可以同时设置中间、左侧、右侧3个标题
plt.plot([1, 1])
# 在右侧底部显示子图标题
plt.title("right bottom",y=0,loc='right')
# 在左侧顶部显示子图标题
plt.title("left top",y=1,loc='left')
# 显示默认子图标题
plt.title("default")
plt.show()

在这里插入图片描述

使用suptitle()设置图像标题

为图像添加一个居中标题。
函数签名为matplotlib.pyplot.suptitle(t, **kwargs)
参数作用及取值如下:

{'fontsize': rcParams['axes.titlesize'],
 'fontweight': rcParams['axes.titleweight'],
 'color': rcParams['axes.titlecolor'],
 'verticalalignment': 'baseline',
 'horizontalalignment': loc}

返回值为Text对象。

suptitle()相关rcParams为:

#figure.titlesize:  large   # size of the figure title (``Figure.suptitle()``)
#figure.titleweight: normal  # weight of the figure title

案例

添加图像标题,并设置坐标、字体大小、文本颜色等属性。

import matplotlib.pyplot as plt

plt.plot([1, 1])
plt.title("title")
plt.suptitle("suptitle", x=0.1, y=0.98, fontsize=16, color='red')

plt.show()

在这里插入图片描述

到此这篇关于matplotlib之pyplot模块之标题(title()和suptitle())的文章就介绍到这了,更多相关matplotlib title()和suptitle()内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: