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'],
'_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参数
plt.text(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()