Matplotlib 找不到基本字体

Posted

技术标签:

【中文标题】Matplotlib 找不到基本字体【英文标题】:Matplotlib cannot find basic fonts 【发布时间】:2017-06-25 02:56:18 【问题描述】:

我在 miniconda 虚拟环境中的 Python 3 上使用 matplotlib 2.0.0 版。我正在开发一个没有 root 权限的 unix 科学计算集群。我通常通过 ipython notebook 执行 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']

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

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

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

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

【问题讨论】:

我遇到了同样的问题。感谢您的提示 fm.get_cachedir(),我正在擦除错误的缓存,现在它可以工作了!谢谢你:) 我有同样的错误,但我的数字最终是衬线字体(使用乳胶)。 尝试重装,matplotlib 2018年发布第三版 【参考方案1】:

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

$ sudo apt install msttcorefonts -qq
$ rm ~/.cache/matplotlib -rf

【讨论】:

可以确认,这在 Ubuntu 16.04 上也适用于我。 已在 Debian Bookworm, matplotlib 3.3.4 上确认【参考方案2】:

我在运行 Ubuntu Xenial 64 位的 Vagrant VM 上遇到了同样的问题。无论我已经安装了多少字体,matplotlib 的“系统”字体名称“sans-serif”都有问题。我通过以下方式修复了它:

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

没有更多关于无衬线的错误消息。

【讨论】:

请注意,您可以通过以下方式获取缓存目录(因操作系统而异):matplotlib.font_manager.get_cachedir() fontmanager.get_cachedir() 对我来说不存在(在 OSX 上),但 fontmanager._fmcache 存在,并删除它指向的 json 文件,解决了问题。【参考方案3】:

这对我有用::

$ sudo apt-get install msttcorefonts -qq

【讨论】:

Docker 用户需要自动接受许可协议:RUN echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selectionsRUN apt-get install -y ttf-mscorefonts-installer 这在我删除了问题中描述的 matplotlib 字体缓存文件后起作用。 你需要清除缓存:rm ~/.cache/matplotlib -fr【参考方案4】:

Windows 用户遇到警告时的解决方案:

UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

    删除位于 matplotlib 缓存中的字体。缓存位置:import matplotlib as mpl; print(mpl.font_manager.get_cachedir())

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

    将Computer Modern等必要字体复制到此目录。

警告可能会持续存在,但您的绘图字体应适当更改。

【讨论】:

完全按照您在 Windows 上为我描述的方式工作(包括持续警告) 该解决方案对我不起作用(module 'matplotlib' has no attribute 'font_manager'),但我发现print(matplotlib.font_manager.findfont('the font i want')) 向我显示了字体(如果找不到我想要的字体,则为默认字体)的位置. 以防万一有人收到此错误:'module' object has no attribute 'font_manager'。我首先像这样导入 font_manager 来解决它:import matplotlib.font_manager as fm 然后:print(fm.get_cachedir())【参考方案5】:

我居然解决了

import matplotlib.font_manager

【讨论】:

【参考方案6】:

它有一个 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

【讨论】:

这是我第一次安装 Windows 时干净且有效的。谢谢:-) 也不必用rm ~/.cache/matplotlib -rf 重新启动jupyter :) +1。非常感谢!我没有删除任何缓存或其他任何东西。刚刚import matplotlib matplotlib.font_manager._rebuild() 解决了我的问题。 不确定这是用于什么版本,但适用于带有 matplotlib 3.5.0 的 Python 3.8.10,AttributeError: module 'matplotlib.font_manager' has no attribute '_rebuild'【参考方案7】:

我在 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'] = 'Serif' 我使用了 plt.rcParams['font.family'] = 'DeJavu Serif' (“DeJavu and serif”之间有一个空格)。我为我工作。

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

【讨论】:

【参考方案8】:

我花了好几个小时才最终弄清楚我需要更新 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

这是为我做的。

【讨论】:

有效!你为我节省了一个小时!我原来的版本是3.0.2,更新到3.1.1后不再弹出Font family ... not found错误信息。【参考方案9】:

我在 gcloud VM 上运行的 Cloud Datalab docker 映像中遇到了类似的问题。执行以下解决了我的问题:

$ sudo apt install msttcorefonts -qq
$ rm ~/.cache/matplotlib -rf

Here 是有关如何获取在包含 Datalab 实例的 gcloud VM 上运行的 docker 映像的说明,以防万一。

【讨论】:

【参考方案10】:

尝试以上所有方法,对我不起作用。

我解决这个问题的方法有点麻烦,因为我在运行 jupyter 的 ubuntu 服务器上使用 conda

locate -b '\mpl-data'

找到一个文件夹

/usr/share/matplotlib/mpl-data

然后我将 simhei 字体添加到 mpl-data 的字体文件夹中。 然后删除 matplotlib 字体缓存

rm -rf ~/.cache/matplotlib

重启jupyter notebook,就可以了。

【讨论】:

【参考方案11】:

我在 macOS 中使用 jupyter notebook,我解决了以下问题,首先关闭你的 jupyter notebook。然后通过在 Python 中执行以下操作找出字体路径

import matplotlib
print(matplotlib.matplotlib_fname())

它为我打印/Users/zyy/anaconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc,注意matplotlibrc 是一个文件,而不是一个目录。

然后下载字体SimHei,复制到上面mpl-data/目录下的fonts/ttf目录下。

删除目录~/.cache/matplotlib 并重启你的jupyter notebook,一切都应该好了。

【讨论】:

【参考方案12】:

对于 Windows 用户

    使用进入matplotlib的缓存目录即可

将 matplotlib 导入为 mpl

打印(mpl.font_manager.get_cachedir())

    清除整个缓存文件

    最后再次导入 matplotlib

希望你也会发现它对我有用

P:S - 如果您希望可以在此之后重新启动您的 jupyter/ide(可选)

【讨论】:

嗨,首先我使用 .getcachedir() 函数找到了位置,之后我从该文件中删除了所有内容,包括空缓存文件和生成的字体。之后我再次启动了我的 Jupiter notebook进口商matplotlib和seaborn,你会发现一旦你再次导入字体会自动重新生成。这有望解决您的问题,因为它解决了我的问题

以上是关于Matplotlib 找不到基本字体的主要内容,如果未能解决你的问题,请参考以下文章

matplotlib的同一注释中的不同字体大小?

matplot:在matplotlib中显示标签的问题[重复]

在Mac OS X 10.6上的Python中,无法在Matplotlib中将字体更改为Helvetica

如何使用 matplotlib blitting 将 matplot.patches 添加到 wxPython 中的 matplotlib 图?

LaTeX 错误:找不到文件“type1cm.sty”

matplot绘图