2017-07-15 69 views
3

我正在嘗試創建具有給定自由度d1和d2的f分佈隨機數,並繪製兩個直方圖與f分佈的隨機數,並繪製理想化的f-分佈曲線,但是當我給df的小值時,直方圖不顯示。我是統計學和matplotlib的新手,我無法弄清楚如何處理這個問題。 這是我的代碼:直方圖不顯示在f分佈圖中

def distF(request, distribution_id): 
    dist = get_object_or_404(Distribution, pk=distribution_id) 
    dfd = dist.var4 
    dfn = dist.var2 
    x = np.random.f(dfn, dfd, size = dist.var3) 
    num_bins = 50 

    fig, ax = plt.subplots() 
    print(x) 
    # the histogram of the data 
    n, bins, patches = ax.hist(x, num_bins, normed=True) 
    y = np.linspace(0, 5, 1001)[1:] 
    dist = st.f(dfn, dfd, 0) 
    #y = np.linspace(st.f.ppf(0.01, dfn, dfd), st.f.ppf(0.99, dfn, dfd), 100) 
    ax.plot(y, dist.pdf(y), '--') 

    ax.set_xlabel('Smarts') 
    ax.set_ylabel('Probability density') 
    ax.set_xlim([0, 4]) 
    ax.set_ylim([0, 3]) 
    fig.tight_layout() 
    canvas = FigureCanvas(fig) 
    response = HttpResponse(content_type='image/png') 
    canvas.print_png(response) 
    plt.close(fig) 
    return response 

這是圖什麼樣子:

F-distribution plot with small df values F-分佈圖與小DF值

F-distribution plot with large df values 大的DF值

F-分佈圖

回答

0

問題是f分佈的dfd爲1對於大數目分佈極大。因此,假設您的數組x中的值爲2000左右,但在0和2000之間只有50個分檔。這使得分檔箱相當大,因此高度相當低。我會認爲,如果你無論如何都想限制你的觀點爲低的數字,最好也將直方圖限制在這個數字。

在下面的代碼中,限制爲5,垃圾箱寬度爲0.2。

import numpy as np 
import scipy.stats as st 
import matplotlib.pyplot as plt 

dfn = 10 
dfd =1 
limit = 5 

x = np.random.f(dfn, dfd, size = 100) 
bins = np.arange(0, limit, 0.2) 

fig, ax = plt.subplots() 

# the histogram of the data 
n, bins, patches = ax.hist(x, bins, normed=True) 
y = np.linspace(0, limit, 1001)[1:] 
dist = st.f(dfn, dfd, 0) 

ax.plot(y, dist.pdf(y), '--') 

ax.set_xlabel('Smarts') 
ax.set_ylabel('Probability density') 
ax.set_xlim([0, limit]) 

fig.tight_layout() 
plt.show() 

enter image description here