Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i am very new with matplotlib library and now i try to generate the bar chart and save as png with this code.

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

The above code when i run it on my macbook it work normally but
when i run it on the server (Ubuntu 18.04.4 LTS) it error like this.

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.

And i have try to fix the problem with this command but it does not work.

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

So are there any way to fix the problem?

like image 956
Kaow Avatar asked Oct 27 '25 06:10

Kaow


1 Answers

Finally i got the answer! to solve the problem is i have to import the ttf font file to matplotlib directly which i will show the solution below.

  1. get the directory of matplotlib library by this code.
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. Past tahoma.ttf to matplotlib directory/fonts/ttf/

  2. delete matplotlib cache

rm -rf ~/.cache/matplotlib

And this is my new code.

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

That all! hope this may help someone who got the same with my issue.

like image 182
Kaow Avatar answered Oct 29 '25 20:10

Kaow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!