浅谈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删除文件示例分享
Jan 28 Python
python通过imaplib模块读取gmail里邮件的方法
May 08 Python
Python中查看文件名和文件路径
Mar 31 Python
python正则中最短匹配实现代码
Jan 16 Python
python针对不定分隔符切割提取字符串的方法
Oct 26 Python
django框架CSRF防护原理与用法分析
Jul 22 Python
python读取ini配置的类封装代码实例
Jan 08 Python
Python编程快速上手——强口令检测算法案例分析
Feb 29 Python
如何实现在jupyter notebook中播放视频(不停地展示图片)
Apr 23 Python
Python填充任意颜色,不同算法时间差异分析说明
May 16 Python
如何基于matlab相机标定导出xml文件
Nov 02 Python
Python实现DBSCAN聚类算法并样例测试
Jun 22 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获得用户使用的代理服务器ip即真实ip
2006/12/31 PHP
什么是MVC,好东西啊
2007/05/03 PHP
php for 循环语句使用方法详细说明
2010/05/09 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(八)
2014/06/23 PHP
php arsort 数组降序排序详细介绍
2016/11/17 PHP
Laravel中服务提供者和门面模式的入门介绍
2017/11/06 PHP
WordPress伪静态规则设置代码实例
2020/12/10 PHP
url 编码 js url传参中文乱码解决方案
2010/04/11 Javascript
js中的this关键字详解
2013/09/25 Javascript
JS中的数组的sort方法使用示例
2014/01/22 Javascript
小米公司JavaScript面试题
2014/12/29 Javascript
jQuery实现给页面换肤的方法
2015/05/30 Javascript
JavaScript中的Promise使用详解
2015/06/24 Javascript
jquery实现仿新浪微博评论滚动效果
2015/08/06 Javascript
JavaScript数据结构链表知识详解
2016/11/21 Javascript
微信小程序小组件 基于Canvas实现直播点赞气泡效果
2020/05/29 Javascript
jQuery实现的下雪动画效果示例【附源码下载】
2018/02/02 jQuery
深入理解JS的事件绑定、事件流模型
2018/05/13 Javascript
JavaScript实现的鼠标跟随特效示例【2则实例】
2018/12/22 Javascript
解决vue-router 二级导航默认选中某一选项的问题
2019/11/01 Javascript
使用vant的地域控件追加全部选项
2020/11/03 Javascript
Python基于递归算法求最小公倍数和最大公约数示例
2018/07/27 Python
使用Python向DataFrame中指定位置添加一列或多列的方法
2019/01/29 Python
django之自定义软删除Model的方法
2019/08/14 Python
常用python爬虫库介绍与简要说明
2020/01/25 Python
pycharm新建Vue项目的方法步骤(图文)
2020/03/04 Python
python thrift 实现 单端口多服务的过程
2020/06/08 Python
python将数据插入数据库的代码分享
2020/08/16 Python
python用Tkinter做自己的中文代码编辑器
2020/09/07 Python
Python 可视化神器Plotly详解
2020/12/26 Python
如何清空Session
2015/02/23 面试题
领导视察欢迎词
2014/01/15 职场文书
《雷雨》教学反思
2014/02/20 职场文书
护校行动方案
2014/05/31 职场文书
中国梦团日活动总结
2014/07/07 职场文书
入党积极分子党支部意见
2015/06/02 职场文书