matplotlib的Text、FontProperties对象、字体(font)属性|中文字体的设置|图像标题、label字体的设置

本文详细介绍了在matplotlib中设置Text对象的字体属性,包括Text的常用性质、FontProperties对象的使用,以及如何通过fontdict和rcParams设置图像标题和label的字体。还特别讨论了在matplotlib中显示中文的方法。

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
仿宋_GB2312FangSong_GB2312
楷体_GB2312KaiTi_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()赋值
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.serifDejaVu 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-serifDejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
font.cursiveApple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, Comic Neue, Comic Sans MS, cursive
font.fantasyChicago, Charcoal, Impact, Western, Humor Sans, xkcd, fantasy
font.monospaceDejaVu 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.titlesizeaxes.set_title‘axes’
axes.titleweightaxes.set_title‘axes’
axes.titlecoloraxes.set_title‘axes’
axes.labelsizeaxes.set_xlabel\ axes.set_ylabel‘axes’
axes.labelweightaxes.set_xlabel\ axes.set_ylabel‘axes’
axes.labelcoloraxes.set_xlabel\ axes.set_ylabel‘axes’
legend.fontsizelegend()‘legend’
legend.title_fontsizelegend()‘legend’
figure.titlesizefigure.suptitle‘figure’
figure.titleweightfigure.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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值