我刚开始从 matlab/mathematica 迁移到 python 进行技术计算。我一直在学习如何使用 matplotlib.pyplot 包,并希望有人可以帮助我使用字体。我最终需要将图形输出保存为可以在 Adobe Illustrator 中打开的 pdf 或 eps 文件。最初,我的 pdf 和 eps 输出包含轮廓字体(而不是保留文本信息的嵌入字体)。按照这个有用的建议,我最终得到了以下代码:
import matplotlib as mpl
import matplotlib.pyplot as plt
# if I omit the next line, the plot saves without error, but with outlined fonts
mpl.rcParams['pdf.fonttype'] = 42 #set Truetype fonts for Adobe
plt.plot(range(5),range(5),'r-')
plt.ylabel('y')
plt.xlabel('x')
plt.title('title')
plt.show()
plt.savefig("myfig.pdf")
但是,当我设置 rcParams['pdf.fonttype']=42 时,最后一行会生成下面复制的错误。谁能指出我正确的方向?我正在使用 Mac OS 10.6 上的 Pyzo 发行版运行 Python 3.3 和 matplotlib 1.2.0。
Traceback (most recent call last):
File "<tmp 1>", line 11, in <module> plt.savefig("myfig.pdf")
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/pyplot.py", line 472, in savefig
return fig.savefig(*args, **kwargs)
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/figure.py", line 1364, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backends/backend_qt4agg.py", line 161, in print_figure
FigureCanvasAgg.print_figure(self, *args, **kwargs)
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backend_bases.py", line 2093, in print_figure
**kwargs)
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backend_bases.py", line 1845, in print_pdf
return pdf.print_pdf(*args, **kwargs)
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backends/backend_pdf.py", line 2307, in print_pdf
file.close()
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backends/backend_pdf.py", line 507, in close
self.writeFonts()
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backends/backend_pdf.py", line 607, in writeFonts
fonts[Fx] = self.embedTTF(realpath, chars[1])
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backends/backend_pdf.py", line 1054, in embedTTF
return embedTTFType42(font, characters, descriptor)
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backends/backend_pdf.py", line 987, in embedTTFType42
self.currentstream.write(unicode_cmap)
File "/Applications/pyzo2013b/lib/python3.3/pyzo-packages/matplotlib/backends/backend_pdf.py", line 379, in write
compressed = self.compressobj.compress(data)
TypeError: 'str' does not support the buffer interface