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
Tim
  • 1,752

4 Answers4

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.

Tim
  • 1,752
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

Sealander
  • 121
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/.

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.