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

Python findfont:找不到字体系列[';Tahoma';]。回到DejaVu Sans,python,matplotlib,Python,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') m

我对
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上运行时正常工作,但
当我在服务器上运行它(Ubuntu18.04.4LTS)时,它会出现如下错误

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,我将在下面展示解决方案

  • 通过此代码获取matplotlib库的目录
  • 经过tahoma.ttf到matplotlib目录/font/ttf/

  • 删除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()
    

    就这些!希望这可以帮助与我的问题相同的人。

    交换可能会解决此问题。@r-初学者感谢您的回复,我会尝试一下,并告诉您结果:)我很高兴您能够解决此问题。@r-初学者非常感谢您的帮助,朋友:D
    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()