I'm trying to have an antialiased render of all text on my plots using Matplotlib. My plots are exported as pdf files. I allowed all parameters that say antialiased in my matplotlibrc file:
### MATPLOTLIBRC FORMAT
lines.antialiased : True # render lines in antialiased (no jaggies)
patch.antialiased : True # render patches in antialiased (no jaggies)
font.family : serif
font.weight : normal
font.size : 12.0
font.serif : Computer modern, DejaVu Serif, Bitstream Vera Serif,New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
text.usetex : True # use latex for all text handling. The following fonts
text.latex.preamble : \usepackage{amsmath},\usepackage{pgfplots},\usepackage[T1]{fontenc}
text.antialiased : True # If True (default), the text will be antialiased.
# This only affects the Agg backend.
mathtext.fontset : cm # Should be 'dejavusans' (default),
# 'dejavuserif', 'cm' (Computer Modern), 'stix',
# 'stixsans' or 'custom'
axes.unicode_minus : True # use unicode for the minus symbol
# rather than hyphen. See
# http://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes
legend.loc : best
legend.frameon : False # if True, draw the legend on a background patch
legend.framealpha : 0.7 # legend patch transparency
legend.facecolor : inherit # inherit from axes.facecolor; or color spec
legend.edgecolor : 0 # background patch boundary color
legend.fancybox : False # if True, use a rounded box for the
# legend background, else a rectangle
figure.autolayout : True # When True, automatically adjust subplot
# parameters to make the plot fit the figure
And here follows a minimum non working example that generates an ugly figure:
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,1],[0,10], linewidth=2, label = r'I am a line')
leg = plt.legend(title=r'$The_{\text{legend}}$ : ' ,ncol=1)
leg._legend_box.align = 'left'
plt.xlabel(r'$D \text{(mm)}$')
plt.ylabel(r'Arbitrary unit')
plt.savefig('example.pdf')
plt.close()
This gives this figure:
Link to the pdf file, only valid 10 days
And when zoomed in we clearly see that the text is really pixelly out of math mode:
How could I avoid this ?
Also:
I know about svg, tikz and pgf export, but I would really like to have a file type that is easy to share with others,
I cannot switch all text to math mode because of typo rules. On plots, variables have to be rendered as maths objects and text as text ones,
adding
\usepackage{lmodern, mathtools}
in the LaTex preamble to use Latin Modern instead of Computer Modern does not work. Maybe because Matplotlib does not use LaTex to render the labels when out of math mode.
usetex=True
somewhere)? In any case what would be the purpose of\text
? Usually to get some upright font in mathmode you would use\mathrm{}
. – ImportanceOfBeingErnest