1
votes

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()

[Output of above code (.png)]

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:

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.

1
The example on the matplotlib page does have everything in sans-serif. Maybe it's time to update your matplotlib version.ImportanceOfBeingErnest
Interesting, thanks for that. I would gladly update my matplotlib, the issue is that the my code is to be shared with people who have the versions listed above and the results must be reproducible solely from running the code (i.e. no additional/manual modifications their end; it is safe to assume a LaTeX installation; slightly less safe to assume the sfmath package, that's just the best compromise I've found so far). Regardless, I think the question still stands by itself.Zatman

1 Answers

1
votes

Well, after playing around with the rcParams for some time, I stumbled upon a way to achieve what I want which

  • does not require an external LaTeX installation
  • does not require manually editing the matplotlibrc file
  • renders math text including greek letters in italic sans-serif font by default
  • renders math text including greek letters in non-italic sans-serif using \mathrm{}

The rcParams settings are changed to:

import matplotlib as mpl
import matploblit.pyplot as plt

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'Arial'
mpl.rcParams['mathtext.fontset'] = 'custom'
mpl.rcParams['mathtext.rm'] = 'sans'
mpl.rcParams['mathtext.it'] = 'sans:italic'
mpl.rcParams['mathtext.default'] = 'it'

fig, ax = plt.subplots()
ax.plot([0,1,2], [2,5,9])
ax.set_xlabel(r'$x$ (m)', fontsize=16)
ax.set_ylabel(r'$y = \alpha$($x$)$x^2-$ $2\mu$', fontsize=16)
ax.set_title(r'Some non-italics: $\mathrm{\alpha, \beta, \gamma}$', fontsize=20)
ax.tick_params(axis='both', which='major', labelsize=16, pad=8)
fig.tight_layout()
fig.show()

[Output of above code (.png)]

Note that I did have to play with the y-axis label in this particular example to make the spacing even.