2016-11-23 68 views
0

我的Python新手,试图重现如下图: enter image description here图表在Python 3.5.2与希腊字母(胶乳)

MWE是:

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() 

和输出 ​​

但是,希腊字母不能正确显示。我可能在这里缺少一些基本的东西。如果有人指导我重现Python 3.5.2中的原始图形,我将非常感激。

编辑

我使用和Python 3.5.2通过Python 2.4.1IDLEUbuntu 16.10

+0

使用Python 3.5.2通过IPython/Anaconda(并通过闲置测试)。将代码复制到提示中时,它工作得很好。你从哪里运行你的? – internetional

+0

@internetional:请参阅我的编辑。任何想法,请。 – MYaseen208

+0

我会尝试IPython,如果我是你。我觉得它比IDLE更稳定。可悲的是我没有什么线索,甚至更少的想法如何解决你的问题。 – internetional

回答

1

可以在matplotlibrc指定字体,如果渲染心不是好的改变乳胶字体:

from matplotlib import rc 

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

或者干脆:标题的

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

输出示例: enter image description here

+0

感谢@Achilles的答案。在'import matplotlib.pyplot as plt'之后,我使用'matplotlib import rc'。但仍然有同样的问题。有什么想法吗。 – MYaseen208

+0

我正在使用'plt.title(r'$ \ mathrm {直方图\ \ IQ:} \ \ mu = 100,\ \ sigma = 15 $')'。 – MYaseen208

+0

不,它不工作。 – MYaseen208