0

I need to put greek letters in labels of a matplotlib plot . how can I do it? For instance, unicode Omega is : u\u03A9. I use plt.xlabel('label')

Cœur
  • 34,719
  • 24
  • 185
  • 251
user129511
  • 111
  • 4
  • Possible duplicate of [Accented characters in Matplotlib](http://stackoverflow.com/questions/2406700/accented-characters-in-matplotlib) – Christian Dean Nov 17 '16 at 00:11
  • what do you get on plot now ? If `plt.xlabel(u'\u03A9')` doesn't work then maybe you need font with this char. – furas Nov 17 '16 at 00:44

2 Answers2

2

If you are looking specifically for greek letter, you can use LaTex in-line math formatting commands (i.e. '$\Omega$') to produce letter that are in the Latex character map.

import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.arange(1000), np.random.rand(1000), 'b.')
plt.xlabel('$\Omega$', size=16)

enter image description here

Here are good resources for finding LaTex symbols.

James
  • 29,484
  • 4
  • 43
  • 62
  • Ok, thanks! It gives me the error `ValueError: invalid \x escape`. Do I need to include some modules? – user129511 Nov 17 '16 at 02:07
  • Without seeing more code, I don't what is causing the error. Did copying the above code not work for you? – James Nov 17 '16 at 02:24
1

I'm not positive about matplotlib, but I would assume that declaring them as unicode strings should work

>>> print u'\u03A9' 
Ω
af3ld
  • 819
  • 7
  • 24
  • what do you mean by declaring them? i put in plot() `''u\u03A9' '` , `u'\u03A9'` `u'\u03A9` but none of them worked :\ – user129511 Nov 17 '16 at 00:32
  • @user129511 I mean putting the `u`in front of the the string like so `u'\u03A9'`. By prefixing the string with a `u`, one is declaring the type of string as unicode. If we were declaring a byte string it would be `b'some bytes'` – af3ld Nov 17 '16 at 18:35