[python] 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)$")
fig.gca().set_ylabel("$lambda$")

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

This question is related to python matplotlib

The answer is


You need to make the strings raw and use latex:

fig.gca().set_ylabel(r'$\lambda$')

As of matplotlib 2.0 the default font supports most western alphabets and can simple do

ax.set_xlabel('?')

with unicode.


If you want tho have a normal string infront of the greek letter make sure that you have the right order:

plt.ylabel(r'Microstrain [$\mu \epsilon$]')

Python 3.x: small greek letters are coded from 945 to 969 so,alpha is chr(945), omega is chr(969) so just type

print(chr(945))

the list of small greek letters in a list:

greek_letterz=[chr(code) for code in range(945,970)]

print(greek_letterz)

And now, alpha is greek_letterz[0], beta is greek_letterz[1], a.s.o


Why not just use the literal characters?

fig.gca().set_xlabel("wavelength, (Å)")
fig.gca().set_ylabel("?")

You might have to add this to the file if you are using python 2:

# -*- coding: utf-8 -*-
from __future__ import unicode literals  # or use u"unicode strings"

It might be easier to define constants for characters that are not easy to type on your keyboard.

ANGSTROM, LAMDBA = "Å?"

Then you can reuse them elsewhere.

fig.gca().set_xlabel("wavelength, (%s)" % ANGSTROM)
fig.gca().set_ylabel(LAMBDA)