matplotlib:font_manager模块FontProperties类的使用(字体属性)

本文介绍了Matplotlib库中FontProperties类的详细用法,包括字体类别、风格、粗细、大小、拉伸和变体等属性,并展示了如何通过实例化创建自定义字体和在图表文本中应用。通过实际案例展示了如何在matplotlib的文本和注解中灵活运用FontProperties对象。
摘要由CSDN通过智能技术生成

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参数。其中fontpropertiesfontfont_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()

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值