33

I have the following axis labels and legend.

plt.ylabel("ratio_2")
plt.xlabel("n_1")
plt.legend(('alpha_1','alpha_2' ), loc = 'best',shadow = True)   
Bruce
  • 33,927
  • 76
  • 174
  • 262

2 Answers2

55

Put dollar signs around the formula: plt.xlabel("$n_1$")

Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
  • 11
    In case the subscript is longer than one character e.g. n_10, then one can write: `plt.xlabel("$n_{10}$")` – Dataman Jul 24 '18 at 10:52
8

The easiest way I know is to enable TeX mode for matplotlib,

from http://www.scipy.org/Cookbook/Matplotlib/UsingTex:

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84
  • 4
    What this does is it runs all your text through TeX, writing various files on the way. Matplotlib has its own TeX-like engine that is faster - just put dollar signs around the formula. – Jouni K. Seppänen Oct 28 '10 at 14:49