【问题标题】:How to make a 3D histogram of a 2D vector distribution in python / plotly如何在 python / plotly 中制作 2D 矢量分布的 3D 直方图
【发布时间】:2020-09-21 07:17:00
【问题描述】:

所以,基本上我有一个 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 轴是 2d 矩阵,z 轴是 pdf:

我想我只需要为列表中的每个向量找到一个好的 pdf 估计值。有没有办法将 KDE 拟合到我的数据中,以获得每个向量的这种近似分布,然后绘制曲面?

非常感谢

【问题讨论】:

  • 您能否在某处提供完整的数据,以便人们尝试使用它?

标签: python 3d statistics plotly visualization


【解决方案1】:

这是一种方法:

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()

结果是:

【讨论】:

  • 这是您要找的吗?
  • 我的荣幸。如果您能接受答案,那就太好了:)
猜你喜欢
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2023-01-12
  • 2016-02-26
  • 2012-12-09
  • 2013-03-09
相关资源
最近更新 更多