Academic Writing Tips: Avoid Using Type 3 Font in Matplotlib and Reduce PDF Size Afterward

Some paper submissions may ask you to not use any Type 3 font in your PDF. In my case, Type 3 font exists in the figures generated by matplotlib in Python. This article summarizes what I did in my submissions for addressing this and relevant issues.

In my paper writing, I usually use matplotlib for plotting experiment figures and insert them in the main paper. However, matplotlib uses type 3 fonts by default for generating figures as PDF. Unfortunately, some paper submissions require that no type 3 font should be used in the final paper PDF. After searching on the Internet (see reference links at the bottom), there appear to be two solutions with different new problems and tradeoffs.

Solution 1

Set the following configurations for matplotlib before plotting your figures. By doing so, it uses type 1 fonts in the generated PDF.

#import matplotlib.pyplot as plt
plt.rcParams["text.usetex"] = True

However, it’s likely that the fonts in your figure become different and something may be off. If that happens and is unacceptable in your case, you may want to take the second solution (which may still not work for you in some cases eventually, unfortunately). Otherwise, this solution gives you the best result in terms of the resulting PDF size and font type compatability.

Solution 2

Alternatively, set the following configurations for telling matplotlib to not use type 3 fonts.

#import matplotlib.pyplot as plt
plt.rcParams['pdf.fonttype'] = 42
plt.rcParams['ps.fonttype'] = 42

This method effectively replaces type 3 fonts with something like CID TrueType and does not change the look of your figures. Yet, the downside is that it will embed the entire font package (not just a subset) in the PDF which results in very large file size.

To embed only a subset of the fonts (the part of the fonts that is used in the figure), we may use the command line tool ghostscript as follows:

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -sOutputFile=out.pdf -dEmbedAllFonts=true -dSubsetFonts=true -dColorConversionStrategy=/LeaveColorUnchanged -dEncodeColorImages=false -dEncodeGrayImages=false -dEncodeMonoImages=false input.pdf

where input.pdf is the original PDF file (supposedly generated from Python with matplotlib) and out.pdf is the output PDF file name. We tell ghostscript to embedded the subset of the fonts and not to touch (compress) any images (by default ghostscript compresses all the images). This way, it should dramatically reduce the PDF size.

However, in my case, this method has an issue with the plots that have filled patterns. Somehow the patterns become so large that they don’t look good in the plots and unfortunately I have not found a way to get around this issue (so I went back to solution 1 for this type of figures).


Was this post helpful?

Leave a Reply

Your email address will not be published.