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)$")

fig.gca().set_ylabel("$lambda$")

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

rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})

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}

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐