首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >3D图和3D直方图子图

3D图和3D直方图子图
EN

Stack Overflow用户
提问于 2019-12-22 16:47:53
回答 1查看 162关注 0票数 1

下面是我的代码:

代码语言:javascript
复制
    import numpy as np
    import math
    from pylab import cm,imshow,colorbar,title,show
    import pylab as pyl

    from mpl_toolkits import mplot3d
    import matplotlib.pyplot as plt


    #Parameters
    N = 10**2                        #Step of discretization

    # Cost function
    T = np.linspace(0,1,N, False) #Discretization of [0,1]
    S = np.linspace(1,2,N,False) #Discretization of [1,2]
    X,Y = np.meshgrid(T,S)
    C = (X-Y)**2                    #Matrix of c[i,j]=(xi-yj)²

    def Sinkhorn(M, r, c, lam):
    """
    Computes the optimal transport matrix and Slinkhorn distance using the
    Sinkhorn-Knopp algorithm

    Inputs:
        - M : cost matrix (n x m)
        - r : vector of marginals (n, )
        - c : vector of marginals (m, )
        - lam : strength of the entropic regularization
        - arret : convergence parameter

    Outputs:
        - P : optimal transport matrix (n x m)
        - dist : Sinkhorn distance
    """

    # Uniform measure over [0;1]
    uni1 = np.ones(N)

    # Uniform measure over [1;2]
    uni2 = np.ones(N)

    n = 1000

    fig = plt.figure()
    ax = plt.axes(projection='3d')
    # I'm going to compute a matrix which is a approximation of a probability over R^{2}
    Gamma_star = Sinkhorn(C, uni1, uni2, 1/10**4)
    ax.scatter(X, Y, Gamma_star)
    plt.title("Gamma bar 1/{} entre une uniforme([0;1]) et uniforme([1;2])".format(1/10**4))    
    plt.show()

我的问题:伽玛条形收敛到我想要调查的度量,所以我想打印子图,类似于这样:(当然它不起作用,它只是告诉你我在想什么)

代码语言:javascript
复制
    for i in range(4):
        plt.subplot(2,2,i+1) 
        Gamma_star = Sinkhorn(C, uni1, uni2, 1/10**i)
        ax.scatter(X, Y, Gamma_star)
        plt.title("Gamma bar 1/{} between uniform([0;1]) and uniform([1;2])".format(1/10**i))    
    plt.plot()

我还想用X,Y和Z= Gamma_bar绘制3D直方图(以同样的方式绘制),如下所示:

我正在努力,如果有人知道如何做到这一点,这将是一个解脱,无论如何,感谢你的帮助。

致以问候。

EN

回答 1

Stack Overflow用户

发布于 2019-12-22 17:06:56

我设法完成了第一部分(图中的子图),这是我的解决方案:

代码语言:javascript
复制
fig = plt.figure()
for i in range(4):
    ax = fig.add_subplot(2, 2, i+1, projection='3d')
    Gamma_star, tqui = Sinkhorn(C, uni1, uni2, 1/10**i)
    ax.scatter(X, Y, Gamma_star)
    plt.title("Gamma bar 1/{} entre une uniforme([0;1]) et uniforme([1;2])".format(1/10**i))

plt.show()

结果是:Output

你有什么想法来改进我的情节吗?可能会添加颜色,可能会更改参数,因为所有的图看起来都是一样的,...欢迎任何帮助:)。

举个例子,我觉得这个颜色很酷:

Colorfull plot

无论如何,现在我将集中在histogram3D子图上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59442800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档