Pregunta

I'm trying to put an upright mu in my axes labels, but matplotlib just shows a square box.

Example code:

# -*- coding: utf-8 -*-
from matplotlib import pyplot as plt

plt.plot([1,2],[3,4])
plt.xlabel(u"A distance (μm)")
plt.show()

The weird thing is: when I run this directly in the interactive python interpreter that spyder has open when it starts, the μ shows up fine. When I run it from a .py file in a dedicated interpreter however, it shows up as '?' or as a square.

What does spyder's python interpreter do that I'm not? What do I need to import to make it work regardless of where the script is run from?

note: I know I could also do something like "A distance ($\mu$m)$, but that creates an italic mu which is typographically incorrect for units...

¿Fue útil?

Solución

The suggestion from Non-ASCII characters in Matplotlib to add this line did the trick:

plt.rc('font', **{'sans-serif' : 'Arial', 'family' : 'sans-serif'})

I thought I already had that covered because I had added the following line to my rcParams dictionary:

plt.rcParams.update({'font.sans-serif': 'Arial', 'font.family': 'sans-serif'})

Not the same thing aparently.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top