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

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的Text、FontProperties对象、字体(font)属性|中文字体的设置|图像标题、label字体的设置

; matplotlib.font_manager.FontProperties对象

看源代码可知,Text的font开头的方法都是通过 调用FontProperties的对应方法实现的。
Text对象也有FontProperties性质,通过set(fontproperties=)或者set_fontproperties()赋值

matplotlib的Text、FontProperties对象、字体(font)属性|中文字体的设置|图像标题、label字体的设置
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)

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 的属性参数,就可以使用这种方法

matplotlib的Text、FontProperties对象、字体(font)属性|中文字体的设置|图像标题、label字体的设置
示例:
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

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'

ax[0].set_title('大阮')

ax[0].set_title('大阮', fontsize=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, seriffont.sans-serifDejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-seriffont.cursiveApple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, Comic Neue, Comic Sans MS, cursivefont.fantasyChicago, Charcoal, Impact, Western, Humor Sans, xkcd, fantasyfont.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('中文')

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

Original: https://blog.csdn.net/Laura_Luo/article/details/124829720
Author: 枯叶酿茶
Title: matplotlib的Text、FontProperties对象、字体(font)属性|中文字体的设置|图像标题、label字体的设置

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/763909/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球