Matplotlib 找不到基本字体

2023-11-30

我在 miniconda 虚拟环境中的 Python 3 上使用 matplotlib 版本 2.0.0。我正在开发一个 UNIX 科学计算集群,但我没有 root 权限。我通常通过 ipython 笔记本执行 python 代码。如果我执行一个基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一条错误消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能够使用 Times New Roman 字体,但即使删除了我从这里找到的字体缓存文件(fontList.py3k.cache):

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

没有效果,我得到与上面相同的错误。 true type 字体目录:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-packages/matplotlib/mpl-data/fonts/ttf/

只有 40 种字体类型:DejaVuSerif、DejaVuSans、STIX、cmb、cmt、cmy

知道会发生什么以及如何添加其他字体吗?谢谢!


为了让它发挥作用,我必须结合当前的两个最佳答案。这对我有用:

$ sudo apt install msttcorefonts -qq
$ rm ~/.cache/matplotlib -rf
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matplotlib 找不到基本字体 的相关文章

随机推荐