开发者

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 "μ":

Matplotlib Unicode axis labels using the Cairo renderer

It works with accented Latin开发者_开发知识库 characters (like "é"). Any ideas?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜