Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeError: Failed to open TrueType font in matplotlib.backends_pdf in python 3.6.1

This code produces a pdf with matplotlib.

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

with PdfPages('test.pdf') as pdf:
    plt.plot([1,4,3])
    pdf.savefig()

On my desktop it works fine, but on my laptop it produces the following messages:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    pdf.savefig()
  File "C:\Users\Georg\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2426, in __exit__
    self.close()
  File "C:\Users\Georg\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2433, in close
    self._file.close()
  File "C:\Users\Georg\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 547, in close
    self.writeFonts()
  File "C:\Users\Georg\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 650, in writeFonts
    fonts[Fx] = self.embedTTF(realpath, chars[1])
  File "C:\Users\Georg\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1124, in embedTTF
    return embedTTFType3(font, characters, descriptor)
  File "C:\Users\Georg\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 910, in embedTTFType3
    filename.encode(sys.getfilesystemencoding()), glyph_ids)
RuntimeError: Failed to open TrueType font

On both systems I run Python 3.6.1 :: Anaconda 4.4.0 (64-bit). The same code ran fine with python 3.5.1 on both systems. OS is Windows 10 Pro and Windows 10 Home.

I have no idea where to look to find a possible installation difference.

like image 857
Gerard Avatar asked Jan 31 '26 05:01

Gerard


1 Answers

I have same problem. Thanks guys from https://github.com/matplotlib/matplotlib/issues/7937 Using the example from the link above, I found a font with which I have correctly saved the pdf. I add 2 strings and all works. For your code it's look as

   import matplotlib.pyplot as plt
   from matplotlib.backends.backend_pdf import PdfPages

   plt.rcParams['pdf.fonttype'] = 42
   plt.rcParams['font.family'] = 'Calibri'

   with PdfPages('test.pdf') as pdf:
      plt.plot([1,4,3])
      pdf.savefig()
like image 162
Slav Nilaskov Avatar answered Feb 01 '26 23:02

Slav Nilaskov



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!