如何在 python / plotly 中制作 2D 矢量分布的 3D 直方图
How to make a 3D histogram of a 2D vector distribution in python / plotly
所以,基本上我在 python 中有一个 2D 向量列表,我想通过 plotly 对该向量的分布(如曲面曲线)进行 3d 可视化。我将留下我的向量的前 4 个组件的示例
[[0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566],
所以我用seaborn.kdeplot()
可视化,只给出了KDE的2D可视化:
但我想要一个 3D 结果,就像在这个双变量正态分布图中,其中 de X 轴和 Y 轴是一个二维矩阵,z 轴是 pdf:
我想我只需要为列表中的每个向量找到一个好的 pdf 估计值。有没有办法让 KDE 适合我的数据,以获得每个向量的近似分布,然后绘制表面?
非常感谢
这是一种方法:
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()
结果是:
所以,基本上我在 python 中有一个 2D 向量列表,我想通过 plotly 对该向量的分布(如曲面曲线)进行 3d 可视化。我将留下我的向量的前 4 个组件的示例
[[0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566], [0.35431211986827776, 0.21438054570807566],
所以我用seaborn.kdeplot()
可视化,只给出了KDE的2D可视化:
但我想要一个 3D 结果,就像在这个双变量正态分布图中,其中 de X 轴和 Y 轴是一个二维矩阵,z 轴是 pdf:
我想我只需要为列表中的每个向量找到一个好的 pdf 估计值。有没有办法让 KDE 适合我的数据,以获得每个向量的近似分布,然后绘制表面?
非常感谢
这是一种方法:
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()
结果是: