Python 直方图不显示在f分布图中
我试图创建具有给定自由度d1和d2的f-分布随机数,并用f-分布随机数绘制直方图,绘制理想化的f-分布曲线,但当我给df的值很小时,直方图不会显示出来。我是统计和matplotlib的新手,不知道如何处理这个问题。 这是我的代码:Python 直方图不显示在f分布图中,python,matplotlib,statistics,distribution,Python,Matplotlib,Statistics,Distribution,我试图创建具有给定自由度d1和d2的f-分布随机数,并用f-分布随机数绘制直方图,绘制理想化的f-分布曲线,但当我给df的值很小时,直方图不会显示出来。我是统计和matplotlib的新手,不知道如何处理这个问题。 这是我的代码: def distF(request, distribution_id): dist = get_object_or_404(Distribution, pk=distribution_id) dfd = dist.var4 dfn = dist
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
这是图的样子:
具有小df值的F分布图
具有大df值的F分布图问题在于,dfd
1的F分布向大数方向扩展。假设数组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()