简体   繁体   English

如何在 python / plotly 中制作二维向量分布的 3D 直方图

[英]How to make a 3D histogram of a 2D vector distribution in python / plotly

So, basically I have a list of 2D vectors in python, and I want to make a 3d visualization of the distribution of this vector, like a surface curve, through plotly.所以,基本上我在 python 中有一个二维向量列表,我想通过 plotly 对该向量的分布进行 3d 可视化,如曲面曲线。 I'll leave a sample of the first 4 components of my vector我将留下我的向量的前 4 个组件的样本

[[0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566],

so I used the seaborn.kdeplot() to visualize, giving only the 2D visualization of the KDE:所以我使用了seaborn.kdeplot()进行可视化,只给出了 KDE 的 2D 可视化:

双变量样本的 KDE 估计器

But i wanted a 3D result, like in this bivariate normal distribution plot, where de X and Y axis are a 2d matrix and the z axis the pdf:但我想要一个 3D 结果,就像在这个二元正态分布 plot 中一样,其中 de X 和 Y 轴是二维矩阵,z 轴是 pdf:

在此处输入图像描述

I think I just need to find a good pdf estimate for each vector in my list.我想我只需要为列表中的每个向量找到一个好的 pdf 估计值。 Is there a way to fit a KDE to my data, in order to obtain this approximated distribution of each vector an then plot the surface?有没有办法将 KDE 拟合到我的数据中,以便获得每个向量的这种近似分布,然后是 plot 表面?

Many thanks非常感谢

Here's a way to do that:这是一种方法:

x = np.random.normal(5, 10, 100000)
y = np.random.normal(10, 3, 100000)
h = np.histogram2d(x, y, bins=50)

def bin_centers(bins):
    centers = (bins + (bins[1]-bins[0])/2) [:-1]    
    return centers

x_bins_centers = bin_centers(h[1])
y_bins_centers = bin_centers(h[2])

df = pd.DataFrame(h[0], index=x_bins_centers, columns=y_bins_centers)
fig = go.Figure(data=[go.Surface(z=df)])
fig.show()

The result is:结果是:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM