浅谈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 相关文章推荐
使用Python3编写抓取网页和只抓网页图片的脚本
Aug 20 Python
更改Ubuntu默认python版本的两种方法python-> Anaconda
Dec 18 Python
Python简单实现自动删除目录下空文件夹的方法
Aug 29 Python
Python爬虫文件下载图文教程
Dec 23 Python
Python实现简单的列表冒泡排序和反转列表操作示例
Jul 10 Python
python爬虫 基于requests模块发起ajax的get请求实现解析
Aug 20 Python
Django stark组件使用及原理详解
Aug 22 Python
python实现代码统计器
Sep 19 Python
python实现用户名密码校验
Mar 18 Python
python opencv把一张图片嵌入(叠加)到另一张图片上的实现代码
Jun 11 Python
Python logging模块异步线程写日志实现过程解析
Jun 30 Python
Python如何爬取51cto数据并存入MySQL
Aug 25 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实现Google plus的好友拖拽分组效果
2016/10/21 PHP
在Laravel5.6中使用Swoole的协程数据库查询
2018/06/15 PHP
PHP实现的只保留字符串首尾字符功能示例【隐藏部分字符串】
2019/03/11 PHP
PHP基于ip2long实现IP转换整形
2020/12/11 PHP
Javascript 陷阱 window全局对象
2008/11/26 Javascript
javascript 面向对象编程 万物皆对象
2009/09/17 Javascript
JQuery index()方法使用代码
2010/06/02 Javascript
Javascript代码在页面加载时的执行顺序介绍
2013/05/03 Javascript
基于jquery插件实现常见的幻灯片效果
2013/11/01 Javascript
javascript 数字格式化输出的实现代码
2013/12/10 Javascript
jquery map方法使用示例
2014/04/23 Javascript
javascript解析json数据的3种方式
2014/05/08 Javascript
谷歌浏览器调试JavaScript小技巧
2014/12/29 Javascript
Jquery 1.9.1源码分析系列(十二)之筛选操作
2015/12/02 Javascript
javascript+html5+css3自定义提示窗口
2017/06/21 Javascript
layui操作列按钮个数和文字颜色的判断实例
2019/09/11 Javascript
原生js实现文件上传、下载、封装等实例方法
2020/01/05 Javascript
设计模式中的原型模式在Python程序中的应用示例
2016/03/02 Python
Python爬虫工程师面试问题总结
2018/03/22 Python
对numpy中轴与维度的理解
2018/04/18 Python
python实现Windows电脑定时关机
2018/06/20 Python
python Web开发你要理解的WSGI & uwsgi详解
2018/08/01 Python
使用python进行拆分大文件的方法
2018/12/10 Python
Python如何操作office实现自动化及win32com.client的运用
2020/04/01 Python
TensorFlow2.X使用图片制作简单的数据集训练模型
2020/04/08 Python
HTML5本地存储之Web Storage详解
2016/07/04 HTML / CSS
Answear匈牙利:来自全球200多个知名时尚品牌
2017/04/21 全球购物
人力管理专业毕业生求职信
2014/02/27 职场文书
环境建设实施方案
2014/03/14 职场文书
法律系毕业生自荐信范文
2014/03/27 职场文书
精彩的演讲稿开头
2014/05/08 职场文书
勤奋学习演讲稿
2014/05/10 职场文书
房产授权委托书范本
2014/09/22 职场文书
2014标准社保办理委托书
2014/10/06 职场文书
小学班主任个人总结
2015/03/03 职场文书
MySQL池化框架学习接池自定义
2022/07/23 MySQL