Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib Unicode axis labels using the Cairo renderer

I'm trying to generate a plot using Matplotlib with a non-Latin character (a "μ") in an axis label, like this:

matplotlib.pyplot.xlabel(u'Sarcomere Length (μm)')

I'm using the Cairo renderer on Linux and I'm getting a "box" instead of "μ":

Incorrect Axis Label

It works with accented Latin characters (like "é"). Any ideas?

like image 607
srunni Avatar asked Mar 23 '11 16:03

srunni


1 Answers

It's a font problem. Whatever font you have set as matplotlib's default doesn't have that particular character. There are a number of ways to potentially fix this, but it's going to be fairly system dependent. (It may be as simple as ensuring that you have the appropriate font package installed.)

You can set the fonts that matplotlib will use in your .matplotlibrc file. If it doesn't find the exact font you're trying to use, you can specify the full path to the appropriate .ttf font file in the .matplotlibrc file. Usually you won't need to do this, though.

However, there's a simpler way, in the particular case you specified above.

Generally speaking, you're better off using matplotlib's mathtext rendering for things like greek symbols. E.g. do matplotlib.pyplot.xlabel(r'Sarcomere Length ($\mu m$)') instead.

like image 70
Joe Kington Avatar answered Sep 23 '22 18:09

Joe Kington