浅谈matplotlib默认字体设置探索


Posted in Python onFebruary 03, 2021

控制默认字体的设置

根据官方文档https://matplotlib.org/tutorials/text/text_props.html#default-font可知:

The base default font is controlled by a set of rcParams

默认字体是由一组rcParams控制的。

rcParam usage
‘font.family' List of either names of font or {‘cursive', ‘fantasy', ‘monospace', ‘sans', ‘sans serif', ‘sans-serif', ‘serif'}
‘font.style' The default style, ex ‘normal', ‘italic'
‘font.variant' Default variant, ex ‘normal', ‘small-caps' (untested)
‘font.stretch' Default stretch, ex ‘normal', ‘condensed' (incomplete)
‘font.weight' Default weight. Either string or integer
‘font.size' Default font size in points. Relative font sizes (‘large', ‘x-small') are computed against this size

我们最关心的当然是'font.family','font.family'的取值有三种:

  • 单一字体名称。
  • 字体名称列表。
  • {'cursive', 'fantasy', 'monospace', 'sans', 'sans serif', 'sans-serif', 'serif'}中的某一个值。

对于字体名称,可以通过ttflist获取。

from matplotlib.font_manager import fontManager 
fontManager.ttflist

对于{'cursive', 'fantasy', 'monospace', 'sans', 'sans serif', 'sans-serif', 'serif'} ,它与实际字体名称之间的映射关系由以下rcParams控制:

family alias rcParam with mappings
‘serif' ‘font.serif'
‘monospace' ‘font.monospace'
‘fantasy' ‘font.fantasy'
‘cursive' ‘font.cursive'
{‘sans', ‘sans serif', ‘sans-serif'} ‘font.sans-serif'

'font.sans-serif'等取值其实都代表一个字体列表。

如何设置默认字体

官方文档给出了设置默认字体的方法建议:

To set the default font to be one that supports the code points you need, prepend the font name to ‘font.family' or the desired alias lists
matplotlib.rcParams[‘font.sans-serif'] = [‘Source Han Sans TW', ‘sans-serif']
or set it in your .matplotlibrc file:
font.sans-serif: Source Han Sans TW, Arial, sans-serif
To control the font used on per-artist basis use the ‘name', ‘fontname' or ‘fontproperties' kwargs documented above.

  • 通过常见的方法设置: matplotlib.rcParams['font.sans-serif'] = ['Source Han Sans TW', 'sans-serif']
  • 设置.matplotlibrc文件

.matplotlibrc文件中的字体设置

配置文件中重要的就是'font.sans-serif'等字体家族列表,列表是有优先级的,越靠前字体的优先级越高,所有很多教程中都要求把需要设置的字体设置为列表的第一个元素。

## ***************************************************************************
## * FONT                                  *
## ***************************************************************************
## The font properties used by `text.Text`.
## See https://matplotlib.org/api/font_manager_api.html for more information
## on font properties. The 6 font properties used for font matching are
## given below with their default values.
##
## The font.family property has five values:
##   - 'serif' (e.g., Times),
##   - 'sans-serif' (e.g., Helvetica),
##   - 'cursive' (e.g., Zapf-Chancery),
##   - 'fantasy' (e.g., Western), and
##   - 'monospace' (e.g., Courier).
## Each of these font families has a default list of font names in decreasing
## order of priority associated with them. When text.usetex is False,
## font.family may also be one or more concrete font names.
##
## The font.style property has three values: normal (or roman), italic
## or oblique. The oblique style will be used for italic, if it is not
## present.
##
## The font.variant property has two values: normal or small-caps. For
## TrueType fonts, which are scalable fonts, small-caps is equivalent
## to using a font size of 'smaller', or about 83%% of the current font
## size.
##
## The font.weight property has effectively 13 values: normal, bold,
## bolder, lighter, 100, 200, 300, ..., 900. Normal is the same as
## 400, and bold is 700. bolder and lighter are relative values with
## respect to the current weight.
##
## The font.stretch property has 11 values: ultra-condensed,
## extra-condensed, condensed, semi-condensed, normal, semi-expanded,
## expanded, extra-expanded, ultra-expanded, wider, and narrower. This
## property is not currently implemented.
##
## The font.size property is the default font size for text, given in pts.
## 10 pt is the standard value.
##
## Note that font.size controls default text sizes. To configure
## special text sizes tick labels, axes, labels, title, etc, see the rc
## settings for axes and ticks. Special text sizes can be defined
## relative to font.size, using the following values: xx-small, x-small,
## small, medium, large, x-large, xx-large, larger, or smaller

#font.family: sans-serif
#font.style:  normal
#font.variant: normal
#font.weight: normal
#font.stretch: normal
#font.size:  10.0

#font.serif:   DejaVu 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-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
#font.cursive:  Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive
#font.fantasy:  Comic Neue, Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy
#font.monospace: DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace

通过rc函数设置默认字体属性的方法

根据文档可知
传统的字体设置方法plt.rcParams['font.sans-serif'] = ['simhei']等价于

font = {'sans-serif' : ['simhei']}
plt.rc('font', **font)
matplotlib.pyplot.rc(group, **kwargs)
Set the current rcParams. group is the grouping for the rc, e.g., for lines.linewidth the group is lines, for axes.facecolor, the group is axes, and so on. Group may also be a list or tuple of group names, e.g., (xtick, ytick). kwargs is a dictionary attribute name/value pairs, e.g.,:
rc('lines', linewidth=2, color='r')
sets the current rcParams and is equivalent to:
rcParams['lines.linewidth'] = 2
rcParams['lines.color'] = 'r'

到此这篇关于浅谈matplotlib默认字体设置探索的文章就介绍到这了,更多相关matplotlib默认字体 内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python函数式编程指南(一):函数式编程概述
Jun 24 Python
python 时间戳与格式化时间的转化实现代码
Mar 23 Python
EM算法的python实现的方法步骤
Jan 02 Python
解决安装tensorflow遇到无法卸载numpy 1.8.0rc1的问题
Jun 13 Python
查看TensorFlow checkpoint文件中的变量名和对应值方法
Jun 14 Python
Django集成搜索引擎Elasticserach的方法示例
Jun 04 Python
Django时区详解
Jul 24 Python
Python中断多重循环的几种方式详解
Feb 10 Python
使用Python内置模块与函数进行不同进制的数的转换
Apr 26 Python
keras中模型训练class_weight,sample_weight区别说明
May 23 Python
Python tempfile模块生成临时文件和临时目录
Sep 30 Python
python中pivot()函数基础知识点
Jan 03 Python
python sleep和wait对比总结
Feb 03 #Python
Python实现简单猜数字游戏
Feb 03 #Python
python 实现图片裁剪小工具
Feb 02 #Python
python向xls写入数据(包括合并,边框,对齐,列宽)
Feb 02 #Python
Python datetime模块的使用示例
Feb 02 #Python
Python基于argparse与ConfigParser库进行入参解析与ini parser
Feb 02 #Python
python中子类与父类的关系基础知识点
Feb 02 #Python
You might like
DC《小丑》11项提名领跑奥斯卡 Netflix成第92届奥斯卡提名最大赢家
2020/04/09 欧美动漫
建立文件交换功能的脚本(一)
2006/10/09 PHP
php addslashes及其他清除空格的方法是不安全的
2012/01/25 PHP
php中filter函数验证、过滤用户输入的数据
2014/01/13 PHP
javascript Ext JS 状态默认存储时间
2009/02/15 Javascript
Aptana调试javascript图解教程
2009/11/30 Javascript
JQuery从头学起第一讲
2010/07/04 Javascript
javascript使用定时函数实现跳转到某个页面
2013/12/25 Javascript
Jquery实现控件的隐藏和显示实例
2014/02/08 Javascript
jquery实现的一个文章自定义分段显示功能
2014/05/23 Javascript
jQuery Migrate 1.1.0 Released 注意事项
2014/06/14 Javascript
浅谈Sizzle的“编译原理”
2015/04/14 Javascript
跟我学习javascript的严格模式
2015/11/16 Javascript
javascript+HTML5 Canvas绘制转盘抽奖
2020/05/16 Javascript
vue.js通过自定义指令实现数据拉取更新的实现方法
2016/10/18 Javascript
微信小程序page的生命周期和音频播放及监听实例详解
2017/04/07 Javascript
jQuery插件select2利用ajax高效查询大数据列表(可搜索、可分页)
2017/05/19 jQuery
JS实现的数组去除重复数据算法小结
2017/11/17 Javascript
JS Object.preventExtensions(),Object.seal()与Object.freeze()用法实例分析
2018/08/25 Javascript
简述ES6新增关键字let与var的区别
2019/08/23 Javascript
原生JS实现天气预报
2020/06/16 Javascript
[10:42]Team Liquid Vs Newbee
2018/06/07 DOTA
[06:45]DOTA2-DPC中国联赛 正赛 Magma vs LBZS 选手采访
2021/03/11 DOTA
与Django结合利用模型对上传图片预测的实例详解
2019/08/07 Python
Python的垃圾回收机制详解
2019/08/28 Python
python2 对excel表格操作完整示例
2020/02/23 Python
python+opencv边缘提取与各函数参数解析
2020/03/09 Python
一款纯css3实现简单的checkbox复选框和radio单选框
2014/11/05 HTML / CSS
美国折扣宠物药房:Total Pet Supply
2018/05/27 全球购物
Myprotein亚太地区:欧洲第一在线运动营养品牌
2020/12/20 全球购物
小学信息技术教学反思
2014/02/10 职场文书
反邪教宣传工作方案
2014/05/07 职场文书
环保倡议书300字
2014/05/15 职场文书
先进典型发言材料
2014/12/30 职场文书
幼儿园教师辞职信
2019/06/21 职场文书
MySQL开启事务的方式
2021/06/26 MySQL