2015-08-28 261 views
4

只需注意,我已經檢查過this questionthis questionSeaborn distplot y軸正常化錯標籤標籤

所以,我使用distplot吸取不同的次要情節有些直方圖:

import numpy as np 
#import netCDF4 as nC# used to get p0_dict 
import matplotlib.pyplot as plt 
from collections import OrderedDict 
import seaborn.apionly as sns 
import cPickle as pickle 

''' 
LINK TO PICKLE 
https://drive.google.com/file/d/0B8Xks3meeDq0aTFYcTZEZGFFVk0/view?usp=sharing 
''' 

p0_dict = pickle.load(open('/path/to/pickle/test.dat', 'r'))  

fig = plt.figure(figsize = (15,10)) 
ax = plt.gca() 
j=1 

for region, val in p0_dict.iteritems(): 

    val = np.asarray(val) 

    subax = plt.subplot(5,5,j) 

    print region 

    try:    
     sns.distplot(val, bins=11, hist=True, kde=True, rug=True, 
        ax = subax, color = 'k', norm_hist=True) 

    except Exception as Ex: 
     print Ex 

    subax.set_title(region) 
    subax.set_xlim(0, 1) # the data varies from 0 to 1 

    j+=1  

plt.subplots_adjust(left = 0.06, right = 0.99, bottom = 0.07, 
        top = 0.92, wspace = 0.14, hspace = 0.6) 

fig.text(0.5, 0.02, r'$ P(W) = 0,1 $', ha ='center', fontsize = 15) 
fig.text(0.02, 0.5, '% occurrence', ha ='center', 
     rotation='vertical', fontsize = 15) 
# obviously I'd multiply the fractional ticklabels by 100 to get 
# the percentage... 

plt.show() 

什麼我希望是KDE曲線下面積總和爲1,和y軸ticklabels反映這個。不過,我得到以下幾點:

enter image description here

正如你所看到的,y軸ticklabels不在範圍[0,1],正如所預期的。打開/關閉norm_histkde不會更改此設置。作爲參考,與兩個輸出關斷:

enter image description here

只是爲了驗證:

aus = np.asarray(p0_dict['AUS']) 
aus_bins = np.histogram(aus, bins=11)[0] 

plt.subplot(121) 
plt.hist(aus,11) 
plt.subplot(122) 
plt.bar(range(0,11),aus_bins.astype(np.float)/np.sum(aus_bins)) 

plt.show() 

enter image description here

在這種情況下的Y ticklabels正確反映這些歸一化的直方圖。

我在做什麼錯?

謝謝你的幫助。

回答

9

y軸是密度,而不是概率。我認爲你期望歸一化的直方圖顯示出一個概率質量函數,其中柱高度的總和等於1。歸一化確保條高度乘以條寬度的總和等於1.這就保證了歸一化直方圖與核密度估計相當,該歸一化直方圖被歸一化,使得曲線下面的面積等於1。

+0

感謝您的澄清@mwaskom。我想我可以添加一個單獨的軸來表示概率?對不起,如果這似乎有點困惑,我不熟悉KDE作爲一種技術。 – areuexperienced

+0

這與KDE本身沒有任何關係。我會閱讀概率和密度之間的區別,這是一個令人困惑的問題,並且經常出現在本站的統計版本上(例如[here](http://stats.stackexchange.com/questions/4220)/can-a-probability-distribution-value-exceed-1-be-ok)),儘管我確信其他地方也有很好的解釋。 – mwaskom