Histogram does not show up in f-distribution plot

Viewed 453

I am trying to create f-distributed random numbers with given degree of freedoms d1 and d2, and plot both a histogram with f-distributed random numbers, and plot an idealised f-distribution curve, but when I give small values to df's, the histogram does not show up. I am new at statistics and matplotlib, and I could not figure out how to deal with this problem. This is my code:

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

This is how the plots look like:

F-distribution plot with small df values F-distribution plot with small df values

F-distribution plot with large df values F-distribution plot with large df values

1 Answers
Related