<>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
pprintimport 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()

技术
今日推荐
PPT
阅读数 121
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信