Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 574
ylangylang Avatar asked Nov 12 '12 04:11

ylangylang


People also ask

Can you use Greek letters as variables in Python?

You can already use Greek letter variable names in Python: https://stackoverflow... | Hacker News. You can already use Greek letter variable names in Python: https://stackoverflow.com/questions/17043894/what-unicode-sy... I've used θ in code dealing with angles before.

How do you write gamma in Python?

Python gamma() is an inbuilt method that is defined under the math module, which is used to find the gamma value of the number parameter passed. For instance, if x is passed as a parameter in gamma function (gamma(x)), it returns the gamma value of the number. We can use the math module by importing it.


1 Answers

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.

like image 91
tacaswell Avatar answered Sep 26 '22 02:09

tacaswell