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个回答

1

我在macOS上使用jupyter notebook,我解决了以下问题:首先关闭jupyter笔记本。然后通过在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笔记本电脑,一切都应该很好。


1
我在运行在gcloud VM上的Cloud Datalab docker镜像中遇到了类似的问题。执行以下命令解决了我的问题:
$ sudo apt install msttcorefonts -qq
$ rm ~/.cache/matplotlib -rf

如果需要,这里有关于如何到达在gcloud VM上运行的Datalab实例的docker镜像的说明。


1

以上方法都对我没用。我不得不结合提供的解决方案。

我在 Windows 10 中使用 PyCharm 运行 Jupyter。以下是我的解决方法。在执行以下步骤之前,请通过 import matplotlib.font_manager 导入字体管理器:

  1. 查找字体目录 => matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
  2. 将您的字体文件(ttf. 文件)放置在该目录中。
  3. 查找字体配置文件:matplotlib.matplotlib_fname()
  4. 编辑以设置字体类型:pdf.fonttype: 42 # 输出类型 3 (Type3) 或 Type 42 (TrueType)
  5. 运行 PyCharm
  6. 启动内核后,缓存文件将被生成。清除 \matplotlib\__pycache__ 文件夹。
  7. 通过以下方式设置要使用的字体:
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Lin Libertine']
  1. 运行代码以生成图表。

如果您重新启动内核,它会生成缓存文件,因此您需要再次删除它们。


0

针对Windows用户

  1. 只需使用以下命令进入matplotlib的缓存目录:

import matplotlib as mpl

print(mpl.font_manager.get_cachedir())

  1. 清除整个缓存文件

  2. 最后再次导入matplotlib

希望这对您有所帮助,因为它对我有效。

P.S. - 如果您愿意,可以在此之后重新启动您的jupyter/ide(可选)。


嗨,首先我使用.getcachedir()函数找到了位置,然后我删除了该文件中的所有内容,包括空缓存文件和生成的字体。之后,我重新启动了我的Jupiter笔记本,并导入了matplotlib和seaborn,你会发现一旦你再次导入,字体会自动重新生成。这将希望解决您的问题,就像它解决了我的问题一样。 - anuroop nag

0

如果你只是想轻松摆脱matplotlib.font_manager的警告,可以这样做:

    # Get rid of findfont: Font family 'Arial' not found.
    # when running a remote notebook on Jupyter Server on Ubuntu Linux server
    import logging
    logging.getLogger("matplotlib.font_manager").setLevel(logging.ERROR)

0

我的情况是我在Docker中运行JupyterHub,我需要使用matplotlib绘制和显示一些中文字符作为标题。

以下是如何安装新字体并使新字体工作的方法(无需重新启动Jupyter容器):

Docker镜像版本:

  • Ubuntu 18.04.3 LTS
  • Python 3.7.6
  • matplotlib.__version__ :3.1.2

所有操作都在容器内执行。

  1. 下载字体到/.../python3.7/site-packages/matplotlib/mpl-data/fonts/ttf

  2. 在笔记本中重新构建字体缓存。~/.cache/matplotlib/AttributeError: module 'matplotlib' has no attribute 'font_manager')。

import matplotlib.font_manager
matplotlib.font_manager._rebuild()

字体缓存以每个用户为基础(/home/{username}/.cache/matplotlib/),因此其他人可能也需要重新构建缓存。

  1. 使用新字体。例如:
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimSun']

在第二步和第三步中,您可能需要重新启动笔记本/内核。

注:

  • 我尝试过sudo apt install msttcorefonts -qq,但我不认为它有效。 我也不知道该软件包安装了什么。 apt-file list msttcorefonts没有返回任何内容。
    为什么apt-file list msttcorefonts没有返回任何内容?

  • 有一个配置文件/.../python3.7/site-packages/matplotlib/mpl-data/matplotlibrc,可以添加font.sans-serif字体。但是,即使不进行修改,新字体也可以使用。


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

0

对于我在Windows 10上使用Pycharm,以上方法都没有完全解决我的问题。

我将Roboto字体放置在文件夹中(请将其替换为您的Python包文件夹): C:\Python\Python310\Lib\site-packages\matplotlib\mpl-data\fonts\ttf\

最后一个让我成功的尝试是删除文件夹中的fontlist-v330.json文件(请将C:\Users\User\替换为您自己的文件夹): C:\Users\User\.matplotlib\
实际上,我尝试了以上方法,因为我找不到缓存文件夹C:\Users\User.cache.matplotlib\,正如本主题中所提到的那样。

有趣的是,如果您在“fontlist-v330.json”文件中找到以下json,则意味着您可以开始使用该字体(在我的情况下是Roboto)。这里还有一个棘手的部分,我还不知道如何解决。我尝试将Arial和Arial Narrow字体都放入文件夹中,但它们两个都似乎在此json文件中使用“Arial”作为它们的名称。因此,我删除了“Arial”字体文件,并保留了那些“Arial Narrow”文件,以便我可以使用“Arial Narrow”字体。

 {
      "fname": "fonts\\ttf\\Roboto-Regular.ttf",
      "name": "Roboto",
      "style": "normal",
      "variant": "normal",
      "weight": 400,
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
    },

然后重新启动 Pycharm,调用函数以设置 rc 参数:

pyplot.rcParams['font.family'] = 'serif'
pyplot.rcParams['font.serif']=['Roboto']

那么它就不会抱怨无法获取字体文件。


0

这两个解决方案都对我没用。我不得不将它们结合起来使用!

仅仅添加以下内容在 Docker 构建过程中失败了。

RUN apt install -y msttcorefonts -qq:

导致以下错误信息:
> #22 11.26 E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/u/ubuntu-advantage-tools/ubuntu-advantage-tools_27.13.6%7e22.04.1_amd64.deb 404  Not Found [IP: 91.189.91.38 80]
#22 11.26 E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
#22 11.26 0 upgraded, 36 newly installed, 0 to remove and 17 not upgraded.
#22 11.26 Need to get 8167 kB of archives.
#22 11.26 After this operation, 37.6 MB of additional disk space will be used.

但要确保更新和修复缺失和损坏的软件包,再加上@Matt KleinsmithDocker用户的评论:

RUN apt update --fix-missing
RUN echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections
RUN apt-get install -y ttf-mscorefonts-installer

通过上述代码,我成功地在容器中运行了它!


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