using the symbol font for Greek symbols in TeX via matplotlib


Question

To annotate my figures with Greek letters in the matplotlib package of Python, I use the following:

import matplotlib
matplotlib.use('PDF')
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
plt.rcParams['ps.useafm'] = True
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
plt.rcParams['pdf.fonttype'] = 42
# plot figure
# ...
# annotate figure
plt.xlabel(r'$\mu$ = 50')
plt.ylabel(r'$\sigma$ = 1.5')

This makes the equal symbol and everything to the right of it in the Helvetica font, as intended, and the Greek symbols default to the usual TeX font (which I believe is Times New Roman.)

How can I make it so the font used for the Greek letters is the "Symbol" font instead? It's important for me not to have it appear in the default Times font of TeX.

thanks for your help.

1
6
1/26/2010 4:37:42 PM

Accepted Answer

Funny you should ask this; I've been struggling with similar problems not too long ago. Considering how complicated font handling is in TeX, I'd sidestep TeX altogether.

However, any decent Helvetica has the Greek letters built-in, so you don't need to use the Symbol font. Just put some Unicode code points into your string, like this:

plt.xlabel(u'\u03bc = 50')
plt.ylabel(u'\u03c3 = 1.5')

For finding the code points, this Unicode codepoint lookup/search tool is really convenient.

I'm not sure if and how matplotlib handles Unicode strings. If the above fails, encode in some encoding that matplotlib expects.

(If you really insist on using Symbol: I don't think you can use multiple fonts within the same label, so then you'll have to add multiple labels and write some code to align them to each other. It's not pretty, but it can be done.)

13
1/16/2017 2:21:11 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon