Matplotlib无法找到基本字体。

99

我正在使用Python 3中的matplotlib版本2.0.0,位于miniconda虚拟环境中。我正在一个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']

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

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

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

有什么想法是怎么回事以及我如何添加其他字体?谢谢!


1
我也遇到了同样的问题。感谢您提供的 fm.get_cachedir() 提示,我之前一直在清除错误的缓存,现在终于解决了!非常感谢 :) - 2D_
我遇到了相同的错误,但我的图形最终以衬线字体显示(使用latex)。 - Soren
尝试重新安装,matplotlib在2018年发布了3版本。 - user3226167
18个回答

110
为了让它起作用,我不得不结合当前两个最佳答案。以下是对我有效的内容:
$ sudo apt install msttcorefonts -qq
$ rm ~/.cache/matplotlib -rf

1
我可以确认,在Ubuntu 16.04上这也适用。 - Joseph Farah
已在Debian Bookworm上确认,matplotlib 3.3.4。 - leitungswasser
1
已在WSL,Ubuntu 20.04上确认。谢谢! - caydin
1
也适用于18.0422.04。谢谢! - Nagabhushan S N
在我的情况下(Mac 用户),它无法工作。我收到了“操作无法完成。无法找到支持 apt 的 Java 运行时。请访问 http://www.java.com 以获取有关安装 Java 的信息。”的错误提示。我已经安装了它,但仍然没有任何变化。有什么建议吗?谢谢。 - R__

39

我在运行Ubuntu Xenial 64位的Vagrant虚拟机中遇到了完全相同的问题。无论我已经安装了多少字体,matplotlib都无法识别“sans-serif”这个“系统字体”名称而出现了问题。 我通过以下方式解决了这个问题:

  • 停止Jupyter
  • 安装字体管理器:sudo apt install font-manager
  • 清理matplotlib缓存目录:rm ~/.cache/matplotlib -fr
  • 重新启动Jupyter。

不会再出现关于sans-serif的错误消息了。


5
请注意,您可以使用以下代码获取缓存目录(因操作系统而异):matplotlib.font_manager.get_cachedir() - DilithiumMatrix
1
fontmanager.get_cachedir() 在我的电脑上(OSX)不存在,但 fontmanager._fmcache 存在,删除它所指向的 JSON 文件解决了这个问题。 - Thomas Ahle

25

这个对我有效:

$ sudo apt-get install msttcorefonts -qq

9
Docker 用户将自动接受许可协议:RUN echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections RUN apt-get install -y ttf-mscorefonts-installer - Matt Kleinsmith
2
在我删除了问题描述中的matplotlib字体缓存文件后,这个问题得到了解决。 - user2699
5
你需要清除缓存:rm ~/.cache/matplotlib -fr - jubueche

18

当Windows用户面对以下警告时,可以采用以下解决方案:

UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))
  1. 删除位于matplotlib缓存中的字体。
    缓存位置:import matplotlib as mpl; print(mpl.font_manager.get_cachedir())

  2. 查找matplotlib的字体目录。路径可能类似于
    C:\Miniconda3\pkgs\matplotlib-2.2.2-py36_1\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

  3. 复制必要的字体如计算机现代体到这个目录。

警告可能仍然存在,但您绘制的字体应该会相应地改变。


3
在 Windows 上,它完全按照你描述的方式运行(包括警告的持续存在)。 - Christian
4
对我来说解决方案行不通(module 'matplotlib' has no attribute 'font_manager'),但我发现print(matplotlib.font_manager.findfont('the font i want'))可以告诉我所需的字体位于哪个位置,如果找不到所需字体则会显示默认字体的位置。 - Mariuslp
1
以防万一有人遇到这个错误:'module' object has no attribute 'font_manager'。我通过首先像这样导入font_manager来解决它: import matplotlib.font_manager as fm 然后: print(fm.get_cachedir()) - Javier TG
对我而言有效的命令是 import matplotlibmatplotlib.get_cachedir(),而无需使用 font_manager - My Work

16

我在使用anaconda env时遇到了这个问题(在其他相关情况下也可能有用)。例如,对于基本环境,我已经检查了以下目录: /home/***/anaconda3/pkgs/matplotlib-base-3.1.3-py37hef1b27d_0/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/

并且注意到我已经安装了DejaVuSerif.ttf,所以我使用了plt.rcParams['font.family'] = 'DeJavu Serif'(“DeJavu”和“Serif”之间有一个空格),而不是plt.rcParams['font.family'] = 'Serif'。这对我很有效。

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'DeJavu Serif'
plt.rcParams['font.serif'] = ['Times New Roman']

经过许多个小时的尝试,这是唯一对我有效的解决方案。谢谢! - Drthm1456
这是正确的答案。 - MarMat

12

有一个conda软件包可供使用[1]。 所以,你不需要使用sudo来修复这个问题!

conda install -c conda-forge -y mscorefonts

更新:下面的功能在最新的matplotlib中已经失效(在v3.4.3上不再起作用)

另外,如果您不想重新启动jupyter,则可以强制重建matplotlib字体缓存

import matplotlib
matplotlib.font_manager._rebuild()

[1] - https://anaconda.org/conda-forge/mscorefonts


2
这很干净,第一次在我的Windows安装中就运行成功了。谢谢 :-) - Paddy3118
1
也不必使用 rm ~/.cache/matplotlib -rf 重新启动 jupyter :) - Vira
1
+1。非常感谢!我没有删除任何缓存或其他东西。只需运行 import matplotlib matplotlib.font_manager._rebuild() 就解决了我的问题。 - Md. Sabbir Ahmed
7
不确定这是哪个版本,但对于Python 3.8.10和Matplotlib 3.5.0,会出现AttributeError: module 'matplotlib.font_manager' has no attribute '_rebuild'的错误。 - sdbbs
2
模块'matplotlib.font_manager'没有属性'_rebuild'。 - CyTex

9

实际上我通过

import matplotlib.font_manager

解决了问题。

属性错误:模块'matplotlib.font_manager'没有属性'_rebuild'。 - CyTex

4

我花了很多时间才最终搞清楚我需要更新matplotlib。

我的初始版本是3.0.3(随jupyter-datascience docker镜像一同提供),它会给我报错,但更新到3.1.1后问题得到解决。

最终,我的docker脚本如下:

RUN pip install matplotlib==3.1.1
COPY fonts /usr/share/fonts/truetype/
RUN fc-cache -fv
RUN rm /home/jovyan/.cache/matplotlib -rf

这就是让我开始接触 IT 技术的原因。


1
它有效了!你节省了我一个小时的时间!我的原始版本是3.0.2,更新到3.1.1之后,不再弹出“找不到字体族...”错误消息。 - allenyllee

3

我只需要删除matplotlib缓存并重新启动jupyter实例即可解决问题:

使用以下命令删除缓存:rm ~/.cache/matplotlib -rf


1

我尝试了以上所有方法,但都没有起作用。

我的解决方法有些不同,因为我在运行jupyter的ubuntu服务器上使用conda。

locate -b '\mpl-data'

找到一个文件夹。
/usr/share/matplotlib/mpl-data

然后我将SimHei字体添加到mpl-data中的字体文件夹中。 然后删除Matplotlib字体缓存。
rm -rf ~/.cache/matplotlib

重新启动Jupyter Notebook,它就可以工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接