浅谈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中黄金分割法实现方法
May 06 Python
python字典多键值及重复键值的使用方法(详解)
Oct 31 Python
Python对象中__del__方法起作用的条件详解
Nov 01 Python
python 使用正则表达式按照多个空格分割字符的实例
Dec 20 Python
Python简单过滤字母和数字的方法小结
Jan 09 Python
python pandas生成时间列表
Jun 29 Python
python多环境切换及pyenv使用过程详解
Sep 27 Python
pytorch中的inference使用实例
Feb 20 Python
记录模型训练时loss值的变化情况
Jun 16 Python
python判断是空的实例分享
Jul 06 Python
python 如何调用远程接口
Sep 11 Python
python中reload重载实例用法
Dec 15 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
PHP中文汉字验证码
2007/04/08 PHP
PHP XML操作类DOMDocument
2009/12/16 PHP
php中长文章分页显示实现代码
2012/09/29 PHP
详谈php静态方法及普通方法的区别
2016/10/04 PHP
Netbeans 8.2与PHP相关的新特性介绍
2016/10/08 PHP
使用composer命令加载vendor中的第三方类库 的方法
2019/07/09 PHP
Laravel使用原生sql语句并调用的方法
2019/10/09 PHP
用方法封装javascript的new操作符(一)
2010/12/25 Javascript
纯文字版返回顶端的js代码
2013/08/01 Javascript
将中国标准时间转换成标准格式的代码
2014/03/20 Javascript
JavaScript实现twitter puddles算法实例
2014/12/06 Javascript
详解JavaScript基于面向对象之创建对象(2)
2015/12/10 Javascript
js+css实现回到顶部按钮(back to top)
2016/03/02 Javascript
纯JS代码实现气泡效果
2016/05/04 Javascript
第六篇Bootstrap表格样式介绍
2016/06/21 Javascript
基于Bootstrap table组件实现多层表头的实例代码
2017/09/07 Javascript
深入理解使用Vue实现Context-Menu的思考与总结
2019/03/09 Javascript
浅谈redux, koa, express 中间件实现对比解析
2019/05/23 Javascript
使用VueRouter的addRoutes方法实现动态添加用户的权限路由
2019/06/03 Javascript
vue 使用element-ui中的Notification自定义按钮并实现关闭功能及如何处理多个通知
2019/08/17 Javascript
Node.js从字符串生成文件流的实现方法
2019/08/18 Javascript
vue实现动态给id赋值,点击事件获取当前点击的元素的id操作
2020/11/09 Javascript
Taro小程序自定义顶部导航栏功能的实现
2020/12/17 Javascript
[06:44]2014DOTA2国际邀请赛-钥匙体育馆开战 开幕式振奋人心
2014/07/19 DOTA
在服务器端实现无间断部署Python应用的教程
2015/04/16 Python
Python RuntimeError: thread.__init__() not called解决方法
2015/04/28 Python
python比较2个xml内容的方法
2015/05/11 Python
Python 实现使用dict 创建二维数据、DataFrame
2018/04/13 Python
django框架自定义模板标签(template tag)操作示例
2019/06/24 Python
关于赌博的检讨书
2014/01/08 职场文书
园林系毕业生求职信
2014/06/23 职场文书
迟到检讨书2000字(精选篇)
2014/10/07 职场文书
领导干部作风建设工作总结
2014/10/23 职场文书
装修公司管理制度
2015/08/05 职场文书
谢师宴家长答谢词
2015/09/30 职场文书
竞选稿之小学班干部
2019/10/31 职场文书