1

When I change the default font that matplotlib uses via

import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{lmodern}')

the filesize of the generated pdf is about twice as large compared to not using the lmodern package (from ~230kb to ~500kb). Any idea why that is, and how to reduce the filesize?

Coeus
  • 43
  • 6
  • 1
    Comments here seem to answer some of this: https://stackoverflow.com/questions/56974665/using-latex-text-rendering-increases-pdf-output-of-matplotlib-by-a-factor-20-is – Alexander L. Hayes Nov 11 '20 at 13:25
  • 1
    The most direct way to reduce filesize is probably choosing jpg or png during savefig. – Alexander L. Hayes Nov 11 '20 at 13:28
  • Thanks, that is indeed the underlying issue. The constant offset of a few 100 kbs makes sense. – Coeus Nov 11 '20 at 14:11
  • 1
    I've been able to reduce the filesize by using the PGF backend of matplotlib: https://matplotlib.org/3.1.1/tutorials/text/pgf.html . Not sure why this worked, though. – Coeus Nov 11 '20 at 14:52
  • Interesting. Are you embedding the PGF directly in TeX documents? It could be saving space by not embedding fonts multiple times. – Alexander L. Hayes Nov 11 '20 at 16:06
  • I haven't tried the direct embedding, yet, since `plt.savefig("figure.pdf", backend='pgf')` actually produces the small pdf file directly. – Coeus Nov 11 '20 at 17:13

1 Answers1

1

As Coeus mentioned in the own comments, the PGF backend yields a smaller PDF size compared to the PDF backend. The reason is that the latter does not embed subsets of fonts. See the open issue on GitHub and my other answer for details.

olegrog
  • 153
  • 2
  • 6