1

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...

jkokorian
  • 2,385
  • 5
  • 27
  • 44
  • Do the terminal and editor you're using support utf-8? Does your locale enable utf8? – Brian Cain Sep 09 '13 at 13:21
  • You can avoid italic characters by writing '$\mathrm{...}$'. Sadly for you, this has no effect on the \mu. You might want to change the LaTeX Preamble to include a package like upgreek to work around that? – Faultier Sep 09 '13 at 13:26
  • Perhaps [this question](http://stackoverflow.com/questions/10960463/non-ascii-characters-in-matplotlib) (and answer) help? –  Sep 09 '13 at 13:32
  • @BrianCain: I don't know... The editor and interpreter do I suppose, since the character shows up ok when I type (ctrl-v) it. How can I check whether or not my locale enables is? – jkokorian Sep 09 '13 at 13:35
  • @Faultier: I know, I've done that in the past, but it's complicated and slower. A unicode μ would just make everything much easier. – jkokorian Sep 09 '13 at 13:36
  • @Evert: No I don't think so. I did everything it says (unless I missed something:)), u in front of the string, magic comment, the lot – jkokorian Sep 09 '13 at 13:37
  • @jkokorian, which OS are you using? `mac`/`linux`/`*nix`? If so, do `locale` or `env|grep -i lang` at a command prompt. – Brian Cain Sep 09 '13 at 13:37
  • @BrianCain windows... – jkokorian Sep 09 '13 at 13:39
  • Sorry to waste your time guys. It turns out I didn't properly switch the font to Arial, I'll answer my own question. – jkokorian Sep 09 '13 at 13:45
  • @jkokorian, glad you figured it out. FYI the windows `cmd.exe` terminal likely does not support utf8 by default. [try changing the code page](http://stackoverflow.com/a/1259468/489590). – Brian Cain Sep 09 '13 at 13:46
  • Thank you all very much for your help! – jkokorian Sep 09 '13 at 13:49

1 Answers1

2

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.

Community
  • 1
  • 1
jkokorian
  • 2,385
  • 5
  • 27
  • 44