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

Kao*_*aow 4 python matplotlib

我对matplotlib库非常陌生,现在我尝试生成条形图并使用此代码另存为 png 。

\n
import matplotlib as mpl\nimport matplotlib.pyplot as plt; plt.rcdefaults()\nimport numpy as np\n\nsite = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp', '\xe0\xb8\x88\xe0\xb8\xb2\xe0\xb8\xa7\xe0\xb8\xb2\xe0\xb8\xaa\xe0\xb8\x84\xe0\xb8\xa5\xe0\xb8\xb4\xe0\xb8\x9b')\nusage = [10,8,6,4,2,1, 2]\n\nmpl.use('Agg')\nmpl.font_manager\nmpl.rc('font',family='Tahoma')\ny_pos = np.arange(len(site))\nplt.bar(y_pos, usage, align='center')\nplt.xticks(y_pos, site)\nplt.ylabel('Percent')\nplt.title('Test')\nplt.tight_layout()\nplt.savefig('test.png')\nplt.cla()\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

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

\n
$ sudo apt install msttcorefonts -qq\n$ rm ~/.cache/matplotlib -rf\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

Kao*_*aow 5

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

\n
    \n
  1. 通过此代码获取 matplotlib 库的目录。
  2. \n
\n
import matplotlib\nprint(matplotlib.matplotlib_fname())\n# output\n# /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc\n# matplotlibrc not the directory, must remove it first\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 将 tahoma.ttf 复制到 matplotlib 目录/fonts/ttf/

    \n
  2. \n
  3. 删除 matplotlib 缓存

    \n
  4. \n
\n
rm -rf ~/.cache/matplotlib\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的新代码。

\n
import matplotlib as mpl\nimport matplotlib.pyplot as plt; plt.rcdefaults()\nimport numpy as np\nimport matplotlib.font_manager as font_manager\nimport os\nsite = (\'Python\', \'C++\', \'Java\', \'Perl\', \'Scala\', \'Lisp\', \'\xe0\xb8\x88\xe0\xb8\xb2\xe0\xb8\xa7\xe0\xb8\xb2\xe0\xb8\xaa\xe0\xb8\x84\xe0\xb8\xa5\xe0\xb8\xb4\xe0\xb8\x9b\')\nusage = [10,8,6,4,2,1, 2]\nmpl.use(\'Agg\')\npath = os.path.join(mpl.rcParams["datapath"], "fonts/ttf/tahoma.ttf")\nprop = font_manager.FontProperties(fname=path)\nplt.rcParams[\'font.family\'] = prop.get_name()\n\ny_pos = np.arange(len(site))\nplt.bar(y_pos, usage, align=\'center\')\ndegrees = 50\nplt.xticks(rotation=degrees)\nplt.xticks(y_pos, site)\nplt.ylabel(\'percent\')\nplt.title(\'test\')\nplt.tight_layout()\nplt.savefig(\'test.png\')\nplt.cla()\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n