gpt4 book ai didi

python - 如何绘制二维直方图?

转载 作者:太空宇宙 更新时间:2023-11-03 18:50:22 27 4
gpt4 key购买 nike

本质上我需要将几个二维直方图添加在一起,但我不知道如何做到这一点。之前,当我对单个直方图执行此操作时,我是这样做的......

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)

这是我的二维直方图代码。

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)

由于代码目前我收到一条错误消息,指出“元组对象不支持项目分配”,我理解该错误消息的含义,但我不确定如何纠正它。如有任何帮助,我们将不胜感激...

最佳答案

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 函数那样的一维数组。

关于python - 如何绘制二维直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18508103/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com