繁体   English   中英

findfont:未找到字体系列 ['Tahoma']。 回到 DejaVu Sans

[英]findfont: Font family ['Tahoma'] not found. Falling back to DejaVu Sans

我对matplotlib库很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

那么有没有办法解决这个问题呢?

终于我得到了答案! 要解决这个问题,我必须将 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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM