Matplotlib不应用字体选择
我在使用matplotlib时遇到了一个关于字体选择的错误,无法解决。每当我在Linux的远程机器上绘图时,就会出现以下错误:
/home/josh/anaconda/envs/py27/lib/python2.7/site-packages/matplotlib/font_manager.py:1236:
UserWarning: findfont: Font family ['Arial'] not found.
Falling back to Bitstream Vera Sans
(prop.get_family(), self.defaultFamily[fontext]))
为了尝试解决这个问题,我编辑了我的.matplotlibrc
文件(为了保险起见,我在两个地方都编辑了):
.matplotlib/.matplotlibrc
.config/matplotlib/.matplotlibrc
在这两个文件中,我添加了以下几行:
backend: Agg
font.family : serif
font.serif : Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman
font.sans-serif : Helvetica, Avant Garde, Computer Modern Sans serif
font.cursive : Zapf Chancery
font.monospace : Courier, Computer Modern Typewriter
text.usetex : true
但是,matplotlib
还是不停地抱怨关于Arial
字体的事。为什么会这样呢?(注意这是在运行一个脚本python my_script.py
时出现的,并没有使用IPython。)
2 个回答
3
来自文档(查看set_family
或set_name
方法的描述)
当text.usetex设置为True时,真实的字体名称是不支持的。
如果你想使用TeX,可能应该选择像'serif'、'sans-serif'、'cursive'、'fantasy'或'monospace'这样的字体系列。
6
如果你在使用seaborn这个库,但你的电脑上没有安装Arial字体,就会出现这个问题(我现在正在努力让这个情况变得更好)。目前最好的解决办法是在绘图之前,先调用一下
sns.set(font="serif")
在导入seaborn之后,绘图之前使用这个方法。