Python 如何绘制布洛赫球体中概率密度函数的三维表示?

Python 如何绘制布洛赫球体中概率密度函数的三维表示?,python,numpy,plot,scipy,probability,Python,Numpy,Plot,Scipy,Probability,我有一个模型,它以特定的形式提供数据集。假设它是一个半径+2个角(r,φ,θ)。我想在bloch球体中绘制这些数据,用两个体积表示25%或75%数据点所在的区域 这里是一个我想在2D中可视化的示例 我的想法是: 使用np.histogramdd对三维数据进行Bin # transform my data onto a cartesian coordinate system X = amp * np.sin(2*chi) * np.cos(theta) Y = amp * np.sin(2*ch

我有一个模型,它以特定的形式提供数据集。假设它是一个半径+2个角(r,φ,θ)。我想在bloch球体中绘制这些数据,用两个体积表示25%或75%数据点所在的区域

这里是一个我想在2D中可视化的示例

我的想法是:

  • 使用np.histogramdd对三维数据进行Bin

    # 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)
    
  • 重新调整装箱数据的形状,以便获得一个按加权索引降序排列的列表,并定义阈值(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)]
    
  • 添加一些打印样式等

    这给了我一个很好的图像,但有些地方出了问题,因为体积不应该在透明球体的外面+它是旋转的。在从垃圾箱到坐标的转换过程中,我似乎搞砸了

  • 有人知道在哪里吗

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

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

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