Tom de Geus
Tom de Geus

Reputation: 5975

Export PDF: font not found

I am trying to export my figure as a PDF. I run into the problem that when I want to edit it (in this case by Graphic on macOS) that the font appears not to be found by the editor. My question is, how do I solve this? Can I 'install' the fonts used by matplotlib?


Example

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([0,1], [0,1])

plt.savefig('test.pdf')

This PDF looks fine in Preview:

enter image description here

But in the editor, it gives gibberish:

enter image description here


Failing solution

Setting

matplotlib.rc("pdf", fonttype=42)

(see this answer).


Partly working solution

What works is to install all matplotlib's fonts. I have followed this answer to find all matplotlib's ttfs, and installed them. This works and solves the problem.

But... This does solve the issues when LaTeX is enabled, by

import matplotlib

matplotlib.rcParams['text.usetex'] = True

How do I install the fonts that matplotlib uses now?

A solution here is to export as SVG. However, for some reason this takes ages on my system (see this bug).

Upvotes: 1

Views: 253

Answers (2)

Tom de Geus
Tom de Geus

Reputation: 5975

I have found a workaround. This is to convert all fonts to outlines before editing. One way to do this is with GhostScript with the option -dNoOutputFonts, as described in this answer:

gs -o file-with-outlines.pdf -dNoOutputFonts -sDEVICE=pdfwrite file.pdf

Upvotes: 1

nahusznaj
nahusznaj

Reputation: 493

What pdf editor are you using? I don't know how to check whether an attempt to solve this is working... but I always use this code to save images in pdf or png:

plt.savefig('test.pdf', edgecolor='none', bbox_inches="tight")

Upvotes: 0

Related Questions