1

I just started migrating from matlab/mathematica to python for technical computing. I have been learning how to use the matplotlib.pyplot package and was hoping someone could help me with fonts. I ultimately need to save graphical output as pdf or eps files that I can open in Adobe Illustrator. Initially, my pdf and eps output contained outlined fonts (rather than embedded fonts retaining text information). Following this helpful advice, I ended up with the following code:

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")

However, when I set rcParams['pdf.fonttype']=42, the final line generates the error copied below. Can anyone point me in the right direction? I am running Python 3.3 and matplotlib 1.2.0, using the Pyzo distribution on Mac OS 10.6.

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
Community
  • 1
  • 1
Mark
  • 11
  • 4
  • Can you test this with 2.x? Can you try with the current master branch from github? – tacaswell May 06 '13 at 03:43
  • I can confirm the same error with python 3.2.3 on Linux. It works fine with python 2.7.3, though. –  May 06 '13 at 04:15
  • @JanHlavacek Can one of you please open an issue on github (if one does not already exist). – tacaswell May 06 '13 at 14:57
  • @JanHlavacek -- Thanks for stepping in there. I was temporarily disappeared at my day job! – Mark May 06 '13 at 23:24

1 Answers1

1

This is a bug in matplotlib and is fixed here: https://github.com/matplotlib/matplotlib/pull/1978#issuecomment-17493157

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • Just confirming: the fix resolves the issue and was tested on Mac OSX with Python 3.3.0 and (citing comment on GitHub) on Linux, with Python 3.2.3. Thanks everyone! – Mark May 07 '13 at 00:02