Matplotlib generated pdf cannot be opened by Acrobat Reader

Viewed 511

I am trying to use serif fonts with latex with matplotlib, unfortunately it does not display in Acrobat Reader, but it is OK on Internet Explorer or Chrome. My minimal code produces an error StandardSymL_Slant_167 not found and varepsilon is not visible, what is missing?

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams.update(mpl.rcParamsDefault)
mpl.rcParams['text.usetex'] = True
mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['times']

fig, ax = plt.subplots()
ax.set_xlabel(r'$x$ $\alpha \varepsilon$')
fig.savefig('test.pdf')

error on acrobat pdf

The output of my enviroment is:

import matplotlib
print(matplotlib.__version__)
3.2.0

edit Found this as well strange:

pdffonts test.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
NimbusRomNo9L-Regu                   Type 1            Custom           yes no  no      21  0
CMMI10                               Type 1            Builtin          yes no  no      13  0
NimbusRomNo9L-ReguItal               Type 1            Custom           yes no  no      25  0
StandardSymL_Slant_167               Type 1            Builtin          yes no  no      17  0
3 Answers

This is not really a nice solution, but I'm not sure a work-around is possible short of manually editing the resulting pdf in a text editor. It seems that this is an error caused by the Adobe Acrobat parser, because matplotlib outputs double precision numbers, whereas Type 1 fonts only support single precision.

Relevant issue which seems it will be fixed in the upcoming version of matplotlib 3.4.0: https://github.com/matplotlib/matplotlib/issues/16087

The fix has since been merged, so updating to the latest master, e.g. with pip install git+https://github.com/matplotlib/matplotlib.git@master should fix it.

As I understand, the problem with Greek fonts. Interesting, that problems usually with win os...

Try win adapted version, please:

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams.update(mpl.rcParamsDefault)

mpl.rcParams['text.usetex'] = True
# mpl.rcParams['font.family'] = ['serif']
# mpl.rcParams['font.serif'] = ['times']
mpl.rcParams['font.family'] = ['times new roman']

fig, ax = plt.subplots()
ax.set_xlabel(r'$x$ $\alpha \varepsilon$')
fig.savefig('test.pdf')

I had a similar issue, I didn't get any informative warnings from pdf readers, but Chrome could display the pdfs. For me the solution was by changing a linewidth that was set to zero. More specifically in my case for fill_between:

plt.fill_between(steps, lower_line, top_line,
                                 linewidth=0.01, # CANNOT BE ZERO, GIVES ERRORS IN PDF READERS
                                 linestyle='dashdot',
                                 antialiased=True,
                                 )
Related