mcglashan mcglashan - 4 months ago 48
Python Question

Typing Greek letters etc. in Python plots

I need to type Greek letters and the Angstrom symbol in labels of axes in a plot. So for example

fig.gca().set_xlabel("$wavelength\, (Angstrom)$")

except that I actually want "Angstrom" and "lambda" replaced by actual symbols. How should I do this? Thanks!


Not only can you add raw strings to matplotlib but you can also specify the font in matplotlibrc or locally with:

from matplotlib import rc

rc('font', **{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)

This would change your serif latex font. You can also specify the sans-serif Helvetica like so


Other options are cursive and monospace with their respective font names. Your label would then be

fig.gca().set_xlabel(r'wavelength $5000 \AA$')

If the font doesn't supply an Angstrom symbol you can try using \mathring{A}