I am generating some plots using Python (2.7) and Matplotlib (1.4.3), and would like all of the text in my plots to be in a sans-serif font. The best I have been able to achieve, using external LaTeX and the sfmath package, fails to set greek letters in sans-serif. This is demonstrated in the following MWE:
import matplotlib.pyplot as plt
from matplotlib import rc
rc('text', usetex=True)
rc('text.latex', preamble=r'\usepackage{sfmath}')
fig, ax = plt.subplots()
ax.plot([0,1,2], [2,5,9]) #arbitrary data
ax.set_xlabel(r'$x$ (m)', fontsize=16)
ax.set_ylabel(r'$y = \alpha(x)x^2 - 2\mu$', fontsize=16)
ax.tick_params(axis='both', which='major', labelsize=16, pad=8)
fig.tight_layout()
fig.show()
I am happy with the example axis labels except for the α and the μ which have been rendered in what looks like the default LaTeX serif font.
Some other answers to questions I have looked at, involving changing various rc settings, which do not solve my problem:
- Sans-serif math with latex in matplotlib
- Upright mu in plot label: retaining original tick fonts
- Typing Greek letters etc. in Python Plots
The only example I have seen which seems to do what I want is here (compare the first and last figures) but this requires modifying the matplotlibrc file. This is not suitable for my purposes; and in any case, I would be surprised if it were not possible to simply set it in the code as required.
(1) How can I modify the above code so that greek letters are displayed in the same sans-serif font as the rest of the text in the plot?*
(2) Is it possible to do all of this using Matplotlib's built-in mathtext rather than relying an external LaTeX installation?
Many thanks in advance for any suggestions.