matplotlib:font_manager模块FontProperties类的使⽤(字体属性)FontProperties类概述
FontProperties类⽤于存储和操作字体的属性。matplotlib⽀持的字体属性基于W3C Cascading Style Sheet, Level 1 font specification,主要有以下6个:字体类别(family)、字体风格(style)、字体粗细(weight)、字体⼤⼩(size)、字体拉伸(stretch)和字体变体(variant)。
FontProperties类签名为:class matplotlib.font_manager.FontProperties(family=None, style=None, variant=None, weight=None, stretch=None,
size=None, fname=None, math_fontfamily=None)
FontProperties类构造函数参数分为:其中字体的6种属性的初始取值均来⾃对应的rcParams参数。
family:字体类别。取值范围为{ 'sans-serif' , 'serif', 'cursive', 'fantasy', 'monospace'},默认值为'sans-serif'。每个取值都代表⼀类字体,在matplotlib中对应数据结构为字体列表,优先级按位置递减。每次在使⽤时,matplotlib会根据rcParams根据字体类别和优先级确定某字体。
family解释
sans-serif⽆衬线体
serif衬线体
cursive⼿写体
fantasy符号字体
monospace等宽字体style:字体风格。取值范围为{ 'normal' , 'italic' , 'oblique'},默认值为'normal'。
style解释
normal正常
italic斜体,包含斜体字符
oblique斜体,简单的倾斜⽂本
variant:字体变体。取值为'normal' (默认)或 'small-caps'(⼩型⼤写字体)
stretch: 字体拉伸。[ 0-1000]的数值或{ 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal' , 'semi-expanded', 'expanded', 'extra-expanded' , 'ultra-expanded'},默认为'normal'。
weight:字体粗细。 [ 0-1000]的数值或{ 'ultralight', 'light', 'normal' , 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'},默认为'normal'。
size:字体⼤⼩。绝对字体⼤⼩或相对⼤⼩{ 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large' },默认值为10。
fname:通过字体⽂件的绝对路径指定特定的字体。值类型为字符串、类路径对象。
总结
根据FontProperties类的构造函数参数可知,⼀⽅⾯FontProperties类可以根据默认的6种字体属性,通过matplotlib的字体查机制使⽤某个字体。另⼀⽅⾯,可以通过fname参数,根据字体名称(前提为该字体已加⼊系统字体列表)、字体的路径指定确定的字体。
案例:演⽰FontProperties对象的属性
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from pprint import pprint
# 默认字体属性
f0 = fm.FontProperties()
# 设置family、size
f1 = fm.FontProperties('simhei',size=20)
# 设置fname、size
f2 = fm.FontProperties(fname='⽅正卡通简体.ttf',size=30)
pprint(vars(f0))
pprint(vars(f1))
pprint(vars(f2))
输出为:
{'_family':['sans-serif'],
'_file': None,
'_size': 10.0,
'_slant':'normal',
'_stretch':'normal',
'_variant':'normal',
'_weight':'normal'}
{'_family':['simhei'],
'_file': None,
'_size': 20.0,
'_slant':'normal',
'_stretch':'normal',
'_variant':'normal',
'_weight':'normal'}
{'_family':['sans-serif'],fontweight属性bold
'_file':'⽅正卡通简体.ttf',
'_size': 30.0,
'_slant':'normal',
'_stretch':'normal',
'_variant':'normal',
'_weight':'normal'}
FontProperties对象的应⽤
在matplotlib中,text()、annoteate()、title()、suptitle()、xlabel()、ylabel()等与⽂本相关的函数均⽀持取值为 Text对象属性的**kwargs参数。其中fontproperties 或 font 或 font_properties参数的取值可以为FontProperties对象。
案例:应⽤FontProperties对象
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
plt.figure(figsize=(13,9))
# 使⽤内置字体名称初始化
f1 = fm.FontProperties('simhei', size=20)
# 使⽤指定字体路径
f2 = fm.FontProperties(fname='⽅正卡通简体.ttf', size=30)
# 验证font参数
<(0.5,0.5,'⽂本', font=f1)
# 验证font_properties参数
plt.annotate('注解',(0.1,0.1), font_properties=f1)
# 验证fontproperties参数
plt.title("标题", fontproperties=f2)
plt.xlabel("x轴", fontproperties=f2)
plt.ylabel("y轴", fontproperties=f2)
plt.show()