4

On a Ubuntu 12.04 machine, whenever I plot something I get an error like:

/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1228: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=medium:stretch=normal:size=x-large. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmb10.ttf

which I cannot find a solution to online. The following code reproduces the error

import pylab as plt
plt.plot(np.random.random((128)))
plt.title("This should be monospaced")
plt.savefig("/tmp/testfig.pdf")

and also the matplotlib self test gives this error:

python -c "import matplotlib as m ; m.test(verbosity=1)"

In ~/.matplotlib/matplotlibrc I have:

font.family         : monospace
font.style          : normal
font.variant        : normal
font.weight         : medium
font.stretch        : normal
font.size           : 10.0
font.serif          : Palatino, Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Charter, serif
font.sans-serif     : Helvetica, Bitstream Vera Sans, sans-serif
font.monospace      : Monaco, Ubuntu Mono Regular, Bitstream Vera Sans Mono, Andale Mono, Nimbus Mono L, Fixed, Terminal, monospace

Some things that do not solve the problem for me:

  1. Delete files in ~/.matplotlib/ (i.e. matplotlibrc, fontList.cache, tex.cache/)
  2. Change the font.family property in ~/.matplotlib/matplotlibrc
  3. Re-install matplotlib after an apt-get purge

The versions I use are:

  • matplotlib 1.1.1~rc1+git20120423-0ubuntu1
  • Python 2.7.3
  • IPython 0.12.1
  • Ubuntu 12.04
1
  • There is no such folder as ~/.matplotlib/ on Ubuntu 16.04 with matplotlib 2.2.3. Where are the cache files I should delete?
    – MERose
    Sep 11, 2018 at 17:50

4 Answers 4

4

After some fiddling around with font packages, the solution for me was installing the following packages:

libsys-cpu-perl pcf2bdf tex-gyre ttf-bitstream-vera tv-fonts xfonts-traditional

I'm not sure why this helps, so perhaps somebody can add some background as to why this helps. Hopefully this helps someone else.

4
  • +1, same problem on ubuntu 12.10 (with mpl in a virtualenv). solved with your answer. thanks.
    – bmu
    Oct 18, 2012 at 14:15
  • 1
    It couldn't find the font. When you installed ttf-bitstream-vera, you installed the font that it wasn't finding.
    – Timtro
    Jul 2, 2013 at 14:27
  • @Timtro but then why does changing font.family not work? Or does it always try to resolve all font families when starting?
    – Tim
    Aug 20, 2013 at 10:16
  • @Tim - Good question. The answer was perfectly clear to me six weeks ago when I replied, but I can't see why at the moment.
    – Timtro
    Aug 21, 2013 at 14:50
2

matplotlib always uses its own set of fonts, independently of other fonts installed on the system. On Ubuntu 12.04 LTS, these fonts are located in /usr/share/matplotlib/mpl-data/fonts/.

When calling a font in your code which is not available in this directory, matplotlib will first try to fall back to Bitstream Vera Sans. However, neither that font is available in .../mpl-data/fonts/ on Ubuntu 12.04 LTS installations. Finally, matplotlib falls back to the cmb10.ttf font, which is available.

Installing system font packages as suggested above will not remedy this warning.

One can, however, add rcParams['mathtext.fontset'] = 'cm' to the code to avoid the multiple fallbacks and warnings.

Alternatively, specify a font which is present in /usr/share/matplotlib/mpl-data/fonts/.

1
  • 2
    Is there a way to add fonts to matplotlib? I tried soft-linking my ttf fonts to /usr/share/matplotlib/mpl-data/fonts/ttf/ with no success.
    – PhilMacKay
    Oct 15, 2014 at 14:19
2

For me on OSX, my Python installation was in my home directory due to admin restrictions which caused font location confusion. I was able to get Helvetica working following these steps: http://blog.olgabotvinnik.com/blog/2012/11/15/2012-11-15-how-to-set-helvetica-as-the-default-sans-serif-font-in/

  1. Download and install Fondu to convert Mac-Helvetica to ttf-Helvetica. With homebrew, this can be done with: brew install fondu.

  2. Find Helvetica on your system. For me, it was /System/Library/Fonts/Helvetica.dfont

  3. Find where matplotlib stores its data. Use python import matplotlib; matplotlib.matplotlib_fname() to print the directory.

  4. Put the .ttf in: your_matplotlib_dir/matplotlib/mpl-data/fonts/ttf then sudo fondu -show /System/Library/Fonts/Helvetica.dfont

  5. Edit your .matplotlibrc file. Copy the original .matplotlibrc file into a personal directory so it doesn’t get over written when updating matplotlib. $ cp your_matplotlib_dir/matplotlib/mpl-data/matplotlibrc ~/.matplotlib/matplotlibrc

  6. Find the line: #font.sans-serif : Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif then uncomment and move Helvetica to the front. font.sans-serif : Helvetica, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

1
  • Please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change.
    – DavidPostill
    Jan 21, 2016 at 22:25
0

Probably it isn't loading your matplotlibrc config file. Make sure it is in the right place. The documentation lists several locations that are searched. Try putting it in one of the locations described:

  • the current working directory of the python process
  • at ~/.config/matplotlib/matplotlibrc

The latter actually did not work for me with debian 7, but neither does ~/.matplotlib/matplotlibrc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .