I am trying to save a matplotlib plot as a pdf page as shown in this example. Everything is fine until tex fonts are used. That is, I can display plots that use tex fonts, but not save them as a pdf. The error trace (shown below) indicates that I am lacking the font called cmr12.

I am running under Ubuntu 16.04 LTS. I have checked that dvipng and gs are installed (and are at the latest version) and the directory where their executables appear is in $PATH. The packages texlive-fonts-recommended and texlive-latex-extra are installed as well, all as per this advice. How do I install the missing font(s)?


  • After not succeeding to install cmr12.ttxthrough the font viewer (see the reply of George and my comment there), I just copied it into /usr/share/fonts/truetype/lyx (where cmr10.ttx already resided) and updated the cache as suggested here. I also cleaned ~/.cache/matplotlib/ and ~/.cache/matplotlib/tex.cache. However, the error persists.
  • I noticed that the file in the trace does not have the .ttf extension... I also noticed that there are files cmr12.pfb and cmr12.pfm in /usr/share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm. I tried copying these files into the directory from which the python script is run under the name cmr12, but that was not liked...

Here is the trace:

Traceback (most recent call last):
  File "temp.py", line 45, in <module>
    d['ModDate'] = datetime.datetime.today()
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 2435, in __exit__
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 2442, in close
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 547, in close
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 647, in writeFonts
    realpath, stat_key = get_realpath_and_stat(filename)
  File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 1038, in __call__
    stat = os.stat(realpath)
OSError: [Errno 2] No such file or directory: '/home/meir/Dropbox/temp/cmr12'

To install:

  1. Get that font here,

  2. After download right click and select open with font viewer

  3. Click install

  4. Now run this: fc-cache -fv to build your font cache files

  • When I clicked on install, it changed to "Install Failed". I downloaded from another mirror, opened in font viewer and the font shows as installed. However, I get the same error trace. – AlwaysLearning Apr 26 '17 at 6:20
  • Please see the edit in the post as well. – AlwaysLearning Apr 26 '17 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.