60

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!

petezurich
  • 7,683
  • 8
  • 34
  • 51
ylangylang
  • 2,884
  • 9
  • 27
  • 32

7 Answers7

94

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.

tacaswell
  • 79,602
  • 19
  • 200
  • 189
28

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}

BandGap
  • 1,645
  • 3
  • 19
  • 25
  • 1
    The Angstrom is not satisfactory with this option, because it appear italic. The $\text{\AA}$ which works in latex does seem to work here. Also there should an option which preserved the font used for these characters. How to do that? – leandro Sep 14 '16 at 15:01
  • 1
    You can use the letter Å instead of a dedicated Ångström symbol (Å), since very few fonts contain the latter, and most ot them use identical glyphs for both, anyway. https://en.wikipedia.org/wiki/%C3%85#Symbol_for_.C3.A5ngstr.C3.B6m – Håken Lid Dec 04 '16 at 10:20
18

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$]')
Frelm
  • 181
  • 1
  • 2
13

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

Dmitriy Simushev
  • 308
  • 1
  • 16
Arpad Kosa
  • 311
  • 3
  • 4
5

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)
Håken Lid
  • 21,116
  • 9
  • 46
  • 63
1

You can just use unicode characters in python. See the following link Unicode characters for engineers in Python .

uoay
  • 136
  • 1
  • 10
0
print('Omega: \u03A9') # Ω
print('Lamda: \u03BB') # λ  

Visit the Website for more symbols More Symbols