2016-07-07 1 views
3

question에 의해 영감을 받아, 어떻게 파이썬에서 같은 종류의 줄거리를 만드나요? 이 플롯은 예상 분포에서 어떻게 벗어 났는지를 시각적으로 표현하는 것을 목표로합니다. 히스토그램 막대를 기대되는 분포 선에 매달므로 기대 값과의 차이는 막대의 위쪽과 예상되는 분포 곡선 사이가 아니라 막대의 아래쪽과 x 축 사이에서 읽습니다.교수형 뿌리 그램을 파이썬으로 그릴 방법은?

내장 함수를 찾을 수 없습니다.

hanging rootogram

답변

7

아이디어는 바의 상단이 예상되는 값입니다 단지 히스토그램 플롯의 각 막대를 이동하는 것입니다 :

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.mlab as mlab 

fig, ax = plt.subplots(1, 2) 
mu = 10 
sig = 0.3 
my_data = np.random.normal(mu, sig, 200) 
x = np.linspace(9, 11, 100) 

# I plot the data twice, one for the histogram only for comparison, 
# and one for the rootogram. 
# The trick will be to modify the histogram to make it hang to 
# the expected distribution curve: 

for a in ax: 
    a.hist(my_data, normed=True) 
    a.plot(x, mlab.normpdf(x, mu, sig)) 
    a.set_ylim(-0.2) 
    a.set_xlim(9, 11) 
    a.hlines(0, 9, 11, linestyle="--") 

for rectangle in ax[1].patches: 

    # expected value in the middle of the bar 
    exp = mlab.normpdf(rectangle.get_x() + rectangle.get_width()/2., mu, sig) 

    # difference to the expected value 
    diff = exp - rectangle.get_height() 
    rectangle.set_y(diff) 

    ax[1].plot(rectangle.get_x() + rectangle.get_width()/2., exp, "ro") 

ax[0].set_title("histogram") 
ax[1].set_title("hanging rootogram") 
plt.tight_layout() 

제공 :

Hanging rootogram python

HTH

관련 문제