MYaseen208 MYaseen208 - 5 days ago 5
LaTeX Question

Graph in Python 3.5.2 with Greek Letters (LaTeX)

I am Python newbie and trying to reproduce the following graph:
enter image description here

MWE is:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
mu, sigma = 100, 15
x = mu + sigma*np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green',
alpha=0.75)
# add a 'best fit' line
y = mlab.normpdf( bins, mu, sigma)
l = plt.plot(bins, y, 'r--', linewidth=1)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()


and output is
enter image description here

However, Greek letters are not rending properly. I might be missing something basic here. I would highly appreciate if someone guide me to reproduce the original graph in
Python 3.5.2
.

Edited

I using
Python 3.5.2
through
Python 2.4.1
and
IDLE
on
Ubuntu 16.10
.

Answer

You can specify the font in matplotlibrcto change the latex font if rendering isnt okay :

from matplotlib import rc

rc('font', **{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)

or simply:

 plt.title("Histogram of IQ: " r'$\mu = 100, \sigma$ =15')

Sample output of the title: enter image description here

Comments