代码之家  ›  专栏  ›  技术社区  ›  tandem

Matplotlib三维曲面图,以第四维为颜色

  •  1
  • tandem  · 技术社区  · 6 年前

    遵循提供的解决方案后 here ,我发现情节不像我想象的那样。

    我想知道问题出在哪里。

    import matplotlib
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    
    x = [10.0, 14.0, 18.0, 14.0, 6.0, 6.0, 2.0, 18.0, 18.0, 6.0, 18.0, 14.0, 10.0, 10.0, 6.0, 6.0, 10.0, 14.0, 2.0, 18.0, 10.0, 14.0]
    y = [1.8, 1.4, 1.2, 2.0, 2.0, 1.4, 2.0, 1.8, 2.0, 1.8, 1.6, 1.8, 2.0, 1.2, 1.6, 1.2, 1.6, 1.2, 1.8, 1.4, 1.4, 1.6]
    z = [1.22, 2.14, 1.66, 0.7, 2.86, 5.89, 3.85, 0.45, 0.4, 4.28, 0.6, 0.92, 0.67, 3.52, 5.25, 4.94, 1.37, 3.76, 4.75, 0.95, 1.99, 1.41]
    z1 = [29.0, 26.72, 26.71, 31.33, 29.46, 24.84, 32.54, 31.43, 33.84, 28.14, 29.84, 31.34, 30.51, 25.0, 25.73, 24.06, 27.09, 26.89, 29.85, 28.93, 26.58, 27.53]
    
    
    # domains
    x = np.array(x)
    y = np.array(y)
    z = np.array(z)
    z1 = np.array(z1)
    
    # convert to 2d matrices
    Z = np.outer(z.T, z)        # 50x50
    Z1 = np.outer(z1.T, z1)        # 50x50
    X, Y = np.meshgrid(x, y)    # 50x50
    
    # fourth dimention - colormap
    # create colormap according to x-value (can use any 50x50 array)
    color_dimension = Z1 # change to desired fourth dimension
    minn, maxx = color_dimension.min(), color_dimension.max()
    norm = matplotlib.colors.Normalize(minn, maxx)
    m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
    m.set_array([])
    fcolors = m.to_rgba(color_dimension)
    
    # plot
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    fig.canvas.draw()
    fig.savefig('test.pdf')
    

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   CodeZero    6 年前

    Z = np.outer(z.T, z)        # 50x50
    

    因此,Z矩阵中的最大值为5.89*5.89=34.69,并且您的绘图似乎是正确的。

    你可能想用 griddata Contour plot of irregularly spaced data .

    如中所述 this post ,也可以使用griddata作为颜色。完整的例子(这里有来自 matplotlib.tri )看起来像这样:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.tri as tri
    from matplotlib.colors import Normalize
    
    x = [10.0, 14.0, 18.0, 14.0, 6.0, 6.0, 2.0, 18.0, 18.0, 6.0, 18.0, 14.0, 10.0, 10.0, 6.0, 6.0, 10.0, 14.0, 2.0, 18.0, 10.0, 14.0]
    y = [1.8, 1.4, 1.2, 2.0, 2.0, 1.4, 2.0, 1.8, 2.0, 1.8, 1.6, 1.8, 2.0, 1.2, 1.6, 1.2, 1.6, 1.2, 1.8, 1.4, 1.4, 1.6]
    z = [1.22, 2.14, 1.66, 0.7, 2.86, 5.89, 3.85, 0.45, 0.4, 4.28, 0.6, 0.92, 0.67, 3.52, 5.25, 4.94, 1.37, 3.76, 4.75, 0.95, 1.99, 1.41]
    z1 = [29.0, 26.72, 26.71, 31.33, 29.46, 24.84, 32.54, 31.43, 33.84, 28.14, 29.84, 31.34, 30.51, 25.0, 25.73, 24.06, 27.09, 26.89, 29.85, 28.93, 26.58, 27.53]
    
    
    # domains
    x = np.array(x)
    y = np.array(y)
    z = np.array(z)
    z1 = np.array(z1)
    
    # Create grid values first.
    ngridx = 100
    ngridy = 100
    xi = np.linspace(x.min(), x.max(), ngridx)
    yi = np.linspace(y.min(), y.max(), ngridy)
    
    # Perform linear interpolation of the data (x,y)
    # on a grid defined by (xi,yi)
    triang = tri.Triangulation(x, y)
    interpolator_z = tri.LinearTriInterpolator(triang, z)
    interpolator_z1 = tri.LinearTriInterpolator(triang, z1)
    Xi, Yi = np.meshgrid(xi, yi)
    zi = interpolator_z(Xi, Yi)
    z1i = interpolator_z1(Xi, Yi)
    
    X, Y, Z, Z1 = xi, yi, zi, z1i
    
    fig = plt.gcf()
    ax1 = fig.add_subplot(111, projection='3d')
    
    minn, maxx = z1.min(), z1.max()
    norm = Normalize()
    surf = ax1.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=cm.jet(norm(Z1)), vmin=minn, vmax=maxx, shade=False)
    
    m = cm.ScalarMappable(cmap=cm.jet)
    m.set_array(Z1)
    col = plt.colorbar(m)
    plt.show()
    

    感谢 ImportanceOfBeingErnest 用于指出matplotlib中的griddata已被弃用,并提供指向当前示例的链接。