您好我想添加到這個代碼中的直方圖的誤差條。我已經看到了一些關於它的帖子,但我沒有發現它們有幫助。這個代碼產生高斯隨機數分佈和核估計適用於它。我需要有errorbars估計直方圖多少是不準確的不斷變化的帶寬如何添加誤差條直方圖在python
from random import *
import numpy as np
from matplotlib.pyplot import*
from matplotlib import*
import scipy.stats as stats
def hist_with_kde(data, bandwidth = 0.3):
#set number of bins using Freedman and Diaconis
q1 = np.percentile(data,25)
q3 = np.percentile(data,75)
n = len(data)**(.1/.3)
rng = max(data) - min(data)
iqr = 2*(q3-q1)
bins =int((n*rng)/iqr)
print(bins)
x = np.linspace(min(data),max(data),200)
kde = stats.gaussian_kde(data,'scott')
kde._compute_covariance()
kde.set_bandwidth()
plot(x,kde(x),'r') # distribution function
hist(data,bins=bins,normed=True) # histogram
data = np.random.normal(0,1,1000)
hist_with_kde(data,30)
show()
我得到的錯誤,如果我執行代碼(在第30行:日期= ...) – bastelflp
您錯過了「;」或第30行的換行符,如下所示:'data = np.random.normal(0,1,1000); hist_with_kde(data,30)' –
我更正了它,謝謝@MikkelBueTellus和bastelflp – jack