此代码生成带有matplotlib的pdf。
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()
在我的桌面上它工作正常,但在我的笔记本电脑上它会产生以下消息:
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
在两个系统上,我运行Python 3.6.1 :: Anaconda 4.4.0(64位)。在两个系统上使用python 3.5.1运行相同的代码。操作系统是Windows 10 Pro和Windows 10 Home。
我不知道在哪里寻找可能的安装差异。
答案 0 :(得分:1)
我有同样的问题。 感谢https://github.com/matplotlib/matplotlib/issues/7937的家伙 使用上面链接中的示例,我找到了一种可以正确保存pdf的字体。 我添加2个字符串,所有作品。对于您的代码,其外观为
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()