2016-11-23 99 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