matplotlib.text.Text对象
见文档描述matplotlib.text
设置Text字体性质的方法有两个:
- Text.set(xxx=value)
- Text.set_xxx(value) 其中xxx指性质名称
Text(matplotlib中)的几个常用性质
一些小记录:serif 衬线体 sans-serif 非衬线体(sans法语当中without的意思)
| 性质(简写) | 描述 | 值 |
|---|---|---|
| fontfamily(family)(fontname) | 字体样式名称 | {‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, ‘monospace’} 和其他的字体名称 |
| fontsize(size) | 字体大小 | float (单位: point) {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’} |
| fontstretch(stretch) | a numeric value in range 0-1000 { ‘ultra-condensed’, ‘extra-condensed’, ‘condensed’, ‘semi-condensed’, ‘normal’, ‘semi-expanded’, ‘expanded’, ‘extra-expanded’, ‘ultra-expanded’} | |
| fontstyle (style) | 字体样式 | {‘normal’, ‘italic’, ‘oblique’} |
| fontvariant(variant) | {‘normal’, ‘small-caps’} | |
| fontweight(weight) | 字体粗细 | a numeric value in range 0-1000 {‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’} |
| horizontalalignment(ha) | 横向位置 | {‘center’, ‘right’, ‘left’} |
| verticalalignment(va) | 纵向位置 | {‘center’, ‘top’, ‘bottom’, ‘baseline’, ‘center_baseline’} |
| rotation | 字体旋转 | float(顺时针旋转角度,非负) {‘vertical’(90°), ‘horizontal’(0°)} |
| rotation_mode | 旋转模式 | {None, ‘default’, ‘anchor’} |
family 可以设置本机上的字体名称,这样可以在matplotlib里面用中文,但是字体名称要用英文,只在本地文件里设置才有效
中文字体英文名称如表 参考博客
| 中文字体名称 | 英文 |
|---|---|
| 宋体 | SimSun |
| 黑体 | SimHei |
| 微软雅黑 | Microsoft YaHei |
| 微软正黑体 | Microsoft JhengHei |
| 新宋体 | NSimSun |
| 新细明体 | PMingLiU |
| 细明体 | MingLiU |
| 标楷体 | DFKai-SB |
| 仿宋 | FangSong |
| 楷体 | KaiTi |
| 仿宋_GB2312 | FangSong_GB2312 |
| 楷体_GB2312 | KaiTi_GB2312 |
| 华文细黑 | STHeiti Light [STXihei] |
| 华文黑体 | STHeiti |
| 华文楷体 | STKaiti |
| 华文宋体 | STSong |
| 华文仿宋 | STFangsong |
| 新宋体 | NSimSun |
| 隶书 | LiSu |
| 幼圆 | YouYuan |
| 华文中宋 | STZhongsong |
| 方正舒体 | FZShuTi |
| 方正姚体 | FZYaoti |
| 华文彩云 | STCaiyun |
| 华文琥珀 | STHupo |
| 华文隶书 | STLiti |
| 华文行楷 | STXingkai |
| 华文新魏 | STXinwei |
font性质示例:代码参考Fonts demo (keyword arguments)

matplotlib.font_manager.FontProperties对象
看源代码可知,Text的font开头的方法都是通过调用FontProperties的对应方法实现的。
Text对象也有FontProperties性质,通过set(fontproperties=)或者set_fontproperties()赋值

FontProperties文档
FontProperties 控制的是“font”组的性质。
FontProperties(family=None, style=None, variant=None, weight=None, stretch=None, size=None, fname=None, math_fontfamily=None)
font属性在图像中的几种设置方法
下面列出我目前接触过的方法,一般用在设置title、label上面
使用Text对象
如果函数能返回一个Text的对象,则可以通过Text对象设置font样式,
titext = ax.set_title(title) # 返回一个Text对象
# 用set()一次性设置多个属性
titext.set(fontsize=11,fontweight='bold',family='serif')
# 或者用下面的语句来设置某一个属性
titext.set_fontsize(11)
titext.set_fontweight('bold')
titext.set_family('serif')
目前所知函数会返回Text对象:axes.Axes.set_title;figure.Figure.suptitle; pyplot.suptitle
直接在函数里面添加font方面参数
如果函数的可选参数表明可以选Text 的属性参数,就可以使用这种方法

示例:
ax.set_title(title, fontsize=12)
ax.set_xlabel(label,fontweight='bold')
fig.suptitle(title,fontfamily='serif')
使用fontdict作为参数传入
使用fontdict一次性设置多个属性值,然后传入多个函数,就可以控制多个函数的字体属性
fontdict = {'fontsize': 10, 'family': ['serif','SimSun'],'weight':'bold'}
ax.set_ylabel(label, fontdict=fontdict)
ax.set_xlabel(中文label, fontdict=fontdict)
ax.set_title(title,fontdict=fontdict)
!! fontdict这种方法似乎对suptitle()函数不起作用
使用FontProperties对象
将FontProperties对象传入函数
from matplotlib.font_manager import FontProperties
# 定义一个FontProperties对象
font0 = FontProperties(family='serif', weight='bold',size=8)
subfigs[0].suptitle(title,fontproperties=font0)
ax.set_title(title ,fontproperties=font0)
ax.set_xlabel('xlabel',fontproperties=font0)
设置全局的font属性来控制全局
全局设置见Customizing Matplotlib with style sheets and rcParams
这里谈谈如何设置字体:
rcParams
font组的设置font.xxx,全局的设置会影响到所有的文字包括但不限于刻度文字,各个标题,各个label,各个图例;
当然设置了全局之后,局部如果设置新的属性值就会被新的属性值覆盖(只作用于局部)
import matplotlib as mpl
mpl.rcParams['font.family'] = ['Times New Roman', 'KaiTi']
mpl.rcParams['font.size'] = 8
mpl.rcParams['font.weight'] = 'bold'
# 画图时就不用再指定font属性
ax[0].set_title('大阮')
# 如果在函数里重新指定了属性就会覆盖默认属性,只作用于此函数
ax[0].set_title('大阮', fontsize=14) # 只有这个为14
font 的字体还有一些具体的属性
| 字体大组 | 具体字体 |
|---|---|
| font.serif | DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif |
| font.sans-serif | DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif |
| font.cursive | Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, Comic Neue, Comic Sans MS, cursive |
| font.fantasy | Chicago, Charcoal, Impact, Western, Humor Sans, xkcd, fantasy |
| font.monospace | DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace |
rc
rc可以把一组 rcParams同时设置,不用一个个写,比如上文又可以写成font组:(使用dict实现)
font = {'family': ['KaiTi', 'Times New Roman'],
'size': 8, 'weight':'bold'}
mpl.rc('font',**font)
非font组的属性
一些特定的属性也可以影响Axes和figure里面的title、label的一些字体属性,使用方法见
| 参数 | 作用函数 | 所属属性组 |
|---|---|---|
| axes.titlesize | axes.set_title | ‘axes’ |
| axes.titleweight | axes.set_title | ‘axes’ |
| axes.titlecolor | axes.set_title | ‘axes’ |
| axes.labelsize | axes.set_xlabel\ axes.set_ylabel | ‘axes’ |
| axes.labelweight | axes.set_xlabel\ axes.set_ylabel | ‘axes’ |
| axes.labelcolor | axes.set_xlabel\ axes.set_ylabel | ‘axes’ |
| legend.fontsize | legend() | ‘legend’ |
| legend.title_fontsize | legend() | ‘legend’ |
| figure.titlesize | figure.suptitle | ‘figure’ |
| figure.titleweight | figure.suptitle | ‘figure’ |
中文的设定
matplotlib里中文的设置只需要设置过中文的字体就可,中文字体的英文名称见上文。
几个设置方法其实同上一节里的几个方法是对应的,family可以同时设置英文和中文字体
-
直接在函数里面添加fontfamily=中文字体
ax[0].set_xlabel('中文',family='SimHei') ax[0].set_title('中文' ,family='YouYuan') -
使用Text对象设置属性family=中文字体
titext = ax.set_title('中文') # 用set()一次性设置多个属性 titext.set(fontsize=11,fontweight='bold',family='SimHei') # 或者 titext.set_family('SimHei') -
使用fontdict设置属性family=中文字体作为参数传入
fontdict = {'fontsize': 10, 'family': ['serif','SimSun'],'weight':'bold'} ax.set_ylabel('label', fontdict=fontdict) ax.set_xlabel('中文', fontdict=fontdict) -
使用FontProperties对象设置属性family=中文字体
from matplotlib.font_manager import FontProperties font0 = FontProperties(family=['serif','SimSun'], weight='bold',size=8) ax.set_ylabel('label', fontproperties=font0) ax.set_xlabel('中文', fontproperties=font0) -
rcParams设置属性family=中文字体
import matplotlib as mpl mpl.rcParams['font.family'] = ['Times New Roman', 'KaiTi'] ax[0].set_title('大阮')
几个和字体设置有关的matplotlib文档
Fonts demo (keyword arguments)
Fonts demo (object-oriented style)使用FontProperties
Configuring the font family
本文详细介绍了在matplotlib中设置Text对象的字体属性,包括Text的常用性质、FontProperties对象的使用,以及如何通过fontdict和rcParams设置图像标题和label的字体。还特别讨论了在matplotlib中显示中文的方法。


属性|中文字体的设置|图像标题、label字体的设置&spm=1001.2101.3001.5002&articleId=124829720&d=1&t=3&u=e85ac3a6d49c43cf97597703a73e07b9)
4147

被折叠的 条评论
为什么被折叠?



