【问题标题】:findfont: Font family ['Tahoma'] not found. Falling back to DejaVu Sansfindfont:找不到字体系列 ['Tahoma']。回到 DejaVu Sans
【发布时间】:2020-09-30 14:55:28
【问题描述】:

我是 matplotlib 库的新手,现在我尝试使用此代码生成条形图并保存为 png。

import matplotlib as mpl
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np

site = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp', 'จาวาสคลิป')
usage = [10,8,6,4,2,1, 2]

mpl.use('Agg')
mpl.font_manager
mpl.rc('font',family='Tahoma')
y_pos = np.arange(len(site))
plt.bar(y_pos, usage, align='center')
plt.xticks(y_pos, site)
plt.ylabel('Percent')
plt.title('Test')
plt.tight_layout()
plt.savefig('test.png')
plt.cla()

当我在我的 macbook 上运行上面的代码时,它可以正常工作,但是
当我在服务器(Ubuntu 18.04.4 LTS)上运行它时,会出现这样的错误。

findfont: Font family ['Tahoma'] not found. Falling back to DejaVu Sans.
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3626 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3623 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3633 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3604 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3637 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3609 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3592 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3607 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3619 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/home/pplus/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py:211: RuntimeWarning: Glyph 3660 missing from current font.
  font.set_text(s, 0.0, flags=flags)
findfont: Font family ['Tahoma'] not found. Falling back to DejaVu Sans.

我已经尝试使用此命令解决问题,但它不起作用。

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

那么有什么办法可以解决这个问题吗?

【问题讨论】:

  • 交换here 可能会解决问题。
  • @r-beginners 谢谢你的回复,我会试一试,然后告诉你结果:)
  • 很高兴您能够解决问题。

标签: python matplotlib


【解决方案1】:

我终于得到了答案!要解决这个问题,我必须直接将 ttf 字体文件导入 matplotlib,我将在下面展示解决方案。

  1. 通过这段代码获取matplotlib库的目录。
import matplotlib
print(matplotlib.matplotlib_fname())
# output
# /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc
# matplotlibrc not the directory, must remove it first
  1. 过去 tahoma.ttf 到 matplotlib 目录/fonts/ttf/

  2. 删除 matplotlib 缓存

rm -rf ~/.cache/matplotlib

这是我的新代码。

import matplotlib as mpl
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.font_manager as font_manager
import os
site = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp', 'จาวาสคลิป')
usage = [10,8,6,4,2,1, 2]
mpl.use('Agg')
path = os.path.join(mpl.rcParams["datapath"], "fonts/ttf/tahoma.ttf")
prop = font_manager.FontProperties(fname=path)
plt.rcParams['font.family'] = prop.get_name()

y_pos = np.arange(len(site))
plt.bar(y_pos, usage, align='center')
degrees = 50
plt.xticks(rotation=degrees)
plt.xticks(y_pos, site)
plt.ylabel('percent')
plt.title('test')
plt.tight_layout()
plt.savefig('test.png')
plt.cla()

就是这样!希望这可以帮助那些对我的问题有同样看法的人。

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 2018-12-25
    • 1970-01-01
    • 2016-10-16
    • 2015-12-07
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2014-05-19
    相关资源
    最近更新 更多