Python Matplotlib 找不到基本字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42097053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:14:57  来源:igfitidea点击:

Matplotlib cannot find basic fonts

pythonmatplotlibtrue-type-fontsminiconda

提问by dylkot

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment. I am working on a unix scientific computing cluster where I don't have root privileges. I am generally executing python code through an ipython notebook. If I do a basic command such as:

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

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

I get an error message:

我收到一条错误消息:

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]))

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:

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

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

The commands:

命令:

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

have no effect, I get the same error as above. The true type fonts directory:

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

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

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy

其中只有 40 种字体:DejaVuSerif、DejaVuSans、STIX、cmb、cmt、cmy

Any idea what could be going on and how I can add additional fonts? Thanks!

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

回答by user3313834

This work for me::

这对我有用::

$ sudo apt-get install msttcorefonts -qq

回答by Craig Kelly

I had this exact same problem on a Vagrant VM running Ubuntu Xenial 64-bit. No matter how many fonts I had already installed, matplotlib was having a problem with the "system" font name "sans-serif". I fixed it by:

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

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

No more error messages about sans-serif.

没有更多关于 sans-serif 的错误消息。

回答by felleg

To get it to work, I had to combine the two current top answers. Here's what worked for me:

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

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

回答by mab

A solution for Windows users, when confronted with the warning:

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

UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))
  1. Delete the fonts located in matplotlib's cache.
    Cache's location: import matplotlib as mpl; print(mpl.font_manager.get_cachedir())

  2. Find matplotlib's font directory. The path might be similar to
    C:\Miniconda3\pkgs\matplotlib-2.2.2-py36_1\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

  3. Copy necessary fonts like Computer Modernto this directory.

  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. 将必要的字体(如Computer Modern)复制到此目录。

The warning may persist, but your plots' font should change appropriately.

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

回答by GRquanti

I actually solved by

我实际上解决了

import matplotlib.font_manager

import matplotlib.font_manager

回答by celine

It took me many hours to finally figure out that I needed to update matplotlib.

我花了好几个小时才终于弄清楚我需要更新 matplotlib。

My original version of 3.0.3(which came with my jupyter-datascience docker image) would give me this error, but updating to 3.1.1fixed it.

我的原始版本3.0.3(随我的 jupyter-datascience docker 图像一起提供)会给我这个错误,但更新以3.1.1修复它。

In the end, my docker script:

最后,我的 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

is what did it for me.

是为我做的。

回答by Soroush Sotoudeh

I was facing a similar issue in a Cloud Datalab docker image running on a gcloud VM. Executing the following fixed the issue for me:

我在 gcloud 虚拟机上运行的 Cloud Datalab docker 映像中遇到了类似的问题。执行以下操作为我解决了这个问题:

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

Hereis instructions on how to get to the docker image running on the gcloud VM containing the Datalab instance just in case.

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

回答by tyan

try all the methods above, not work for me.

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

my way to solve this is a little dirrent cause I'm using conda on ubuntu server running jupyter

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

locate -b '\mpl-data'

find a folder

找到一个文件夹

/usr/share/matplotlib/mpl-data

then I add the simhei fonts into the font folder in mpl-data . then remove matplotlib fonts cache

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

rm -rf ~/.cache/matplotlib

restart jupyter notebook, and it works.

重新启动 jupyter notebook,它可以工作。

回答by zyy

I am in macOS with jupyter notebook, I solved with the following, first close your jupyter notebook. Then find out the font path by doing the following in Python

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

import matplotlib
print(matplotlib.matplotlib_fname())

it prints /Users/zyy/anaconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrcfor me, notice matplotlibrcis a file, not a directory.

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

Then download font SimHei, and copy it to the directory fonts/ttfunder the mpl-data/directory above.

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

Delete directory ~/.cache/matplotliband restart your jupyter notebook, everything should be good.

删除目录~/.cache/matplotlib并重新启动您的 jupyter notebook,一切都应该没问题。