基本上我需要在一起添加几个2d直方图,但我不知道如何做到这一点。在我为一个直方图做之前我就这样做了......
enter for i in range(0,BMI[ind_2008].shape[0]):
id_temp=ID[ind_2008[i]]
ind_2009_temp=np.where(ID[ind_2009] == id_temp)
actual_diff=BMI[ind_2008[i]]-BMI[ind_2009[ind_2009_temp]]
diff=np.abs(BMI[ind_2008][i]-BMI_p1)
pdf_t, bins_t=np.histogram(diff,bins=range_v-1,range=(0,range_v))
if i == 0:
pdf=pdf_t
pdf[:]=0.0
pdf=pdf+pdf_t
bincenters = 0.5*(bins_t[1:]+bins_t[:-1])
fig3=plt.figure()
plt.plot(bincenters,pdf)
这是我对2d直方图的代码。
for i in range(0,BMI[ind_2008].shape[0]):
diff_BMI=np.abs(BMI[ind_2008][i]-BMI_p1)
diff_DOB=np.abs(dob_j[ind_2008][i]-dob_jwp1)
hist=np.histogram2d(diff_BMI,diff_DOB,bins=(35,1000))
if i == 0:
pdf=hist
pdf[:]=0.0
pdf=pdf+hist
fig3=plt.figure()
plt.plot(pdf)
由于代码目前我收到一条错误消息,提示'元组对象不支持项目分配'我理解错误消息的含义但我不知道如何纠正它。任何帮助表示赞赏...
答案 0 :(得分:1)
histogram2d
函数返回三元组:
H : ndarray, shape(nx, ny)
The bi-dimensional histogram of samples `x` and `y`. Values in `x`
are histogrammed along the first dimension and values in `y` are
histogrammed along the second dimension.
xedges : ndarray, shape(nx,)
The bin edges along the first dimension.
yedges : ndarray, shape(ny,)
The bin edges along the second dimension.
所以你的函数调用应该如下:
H, xedges, yedges = np.histogram2d(diff_BMI, diff_DOB, bins=(35,1000))
然后你可以使用直方图H
进行操作。但请记住,它是一个二维数组,而不是一维数组,如np.histogram
函数的情况。