首页 > 解决方案 > 如何绘制布洛赫球中概率密度函数的 3_d 表示?

问题描述

我有一个模型,它为我提供特定形式的数据集。可以说它的半径+ 2个角度(r,phi,theta)。我想在 bloch 球体中绘制这些数据,其中两个体积表示 25% 或 75% 的数据点所在的区域。

这是我想在 2D 中可视化的示例。

这是我想在 2D 中可视化的示例。

我的想法是:

  1. 使用 np.histogramdd 对 3D 数据进行分箱

    # transform my data onto a cartesian coordinate system
    X = amp * np.sin(2*chi) * np.cos(theta)
    Y = amp * np.sin(2*chi) * np.sin(theta)
    Z = amp * np.cos(2*chi)
    
    data_xyz=np.array([X,Y,Z]).T
    
    hist, binedges = np.histogramdd(data_xyz,bins=60, normed=False)
    
    
    list_of_weighted_entries = np.sort(np.reshape(hist,np.power(60,3)))
    weight_sum = np.sum(hist)
    
  2. 重塑分箱数据,以便我得到一个加权索引降序排列的列表,并定义阈值(25 或 75%)以定义哪些索引位于特定的 pdf 卷内:

    #For 25 %
    cumu_weight = 0
    index = 1
    while cumu_weight < weight_sum * 0.25:
        cumu_weight = cumu_weight + list_of_weighted_entries[-index]
        index = index + 1
    
    treshhold_value = list_of_weighted_entries[-(index-1)]
    
  3. 使用行进立方体来绘制这些体积

    #calculate the bin_size for each direction
    diff0 = binedges[0][1] - binedges[0][0]
    diff1 = binedges[1][1] - binedges[1][0]
    diff2 = binedges[2][1] - binedges[2][0]
    
    verts, faces, normals, values = measure.marching_cubes_lewiner(hist, treshhold_value,(diff0,diff1,diff2))
    
    fig  = plt.figure(figsize=(10, 10))
    ax   = fig.add_subplot(111, projection='3d')
    mesh = Poly3DCollection(verts[faces],linewidths=0,facecolor="rosybrown")
    mesh.set_edgecolor('k')
    ax.add_collection3d(mesh)
    
  4. 添加一些绘图样式等......

    这给了我一个非常漂亮的图像,但是出了点问题,因为体积不应该在透明球体之外+它是旋转的。在从垃圾箱转换回坐标的某个地方,我似乎搞砸了。

有人知道在哪里吗?

编辑:我现在很确定问题在于Poly3DCollection绘图。问题是这个图总是从 0:0:0 开始,但我给 histogramm 函数的坐标也有负体积->我必须在计算“PDF 体积”后为我得到的数据添加翻译

这给了我一个非常漂亮的图像,但是出现了问题,因为体积不应该在透明球体之外(因为球体的半径是最大半径的 1.1 倍)。

在此处输入图像描述

标签: pythonnumpyplotscipyprobability

解决方案


推荐阅读