浅谈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 StringIO模块实现在内存缓冲区中读写数据
Apr 08 Python
Python中类型关系和继承关系实例详解
May 25 Python
Django中使用celery完成异步任务的示例代码
Jan 23 Python
Python中的函数作用域
May 07 Python
Python统计python文件中代码,注释及空白对应的行数示例【测试可用】
Jul 25 Python
使用Python的SymPy库解决数学运算问题的方法
Mar 27 Python
opencv实现简单人脸识别
Feb 19 Python
在Django下测试与调试REST API的方法详解
Aug 29 Python
python基于opencv 实现图像时钟
Jan 04 Python
python中spy++的使用超详细教程
Jan 29 Python
一篇文章带你了解Python和Java的正则表达式对比
Sep 15 Python
python基础之函数的定义和调用
Oct 24 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注销代码(session注销)
2012/05/31 PHP
ThinkPHP登录功能的实现方法
2014/08/20 PHP
TP5框架使用QueryList采集框架爬小说操作示例
2020/03/26 PHP
Jquery中children与find之间的区别详细解析
2013/11/29 Javascript
鼠标经过tr时,改变tr当前背景颜色
2014/01/13 Javascript
jQuery实现点击该行即可删除HTML表格行
2014/10/17 Javascript
jQuery元素选择器用法实例
2014/12/23 Javascript
jquery利用命名空间移除绑定事件的方法
2015/03/11 Javascript
如何用JavaScript实现动态修改CSS样式表
2016/05/20 Javascript
javaScript事件学习小结(四)event的公共成员(属性和方法)
2016/06/09 Javascript
URL的参数中有加号传值变为空格的问题(URL特殊字符)
2016/11/04 Javascript
Vue编写多地区选择组件
2017/08/21 Javascript
vue路由跳转传递参数的方式总结
2020/05/10 Javascript
[42:24]完美世界DOTA2联赛循环赛 LBZS vs DM BO2第一场 11.01
2020/11/02 DOTA
python代码检查工具pylint 让你的python更规范
2012/09/05 Python
Python使用smtplib模块发送电子邮件的流程详解
2016/06/27 Python
浅谈django中的认证与登录
2016/10/31 Python
python实现杨辉三角思路
2017/07/14 Python
python TF-IDF算法实现文本关键词提取
2019/05/29 Python
python中列表的切片与修改知识点总结
2019/07/23 Python
python 实现关联规则算法Apriori的示例
2020/09/30 Python
利用CSS3实现文字折纸效果实例代码
2018/07/10 HTML / CSS
韩国休闲女装品牌网站:ANAIS
2016/08/24 全球购物
Clarins娇韵诗英国官网:来自法国的天然护肤品牌
2017/04/18 全球购物
Amara美国站:英国高端家居礼品网站,世界各地的奢侈家具品牌
2017/07/26 全球购物
美国婴儿用品店:Babies”R”Us
2017/10/12 全球购物
淘宝店策划方案
2014/06/07 职场文书
乡镇创先争优活动总结
2014/08/28 职场文书
好人好事演讲稿
2014/09/01 职场文书
计算机科学与技术专业求职信
2014/09/03 职场文书
单位在职证明书
2014/09/11 职场文书
党的群众路线教育实践活动整改落实情况自查报告
2014/10/28 职场文书
2015年银行大堂经理工作总结
2015/04/24 职场文书
铁人纪念馆观后感
2015/06/16 职场文书
企业管理制度设计时要注意的几种“常见病”!
2019/04/19 职场文书
MySQL创建表操作命令分享
2022/03/25 MySQL