Matplotlib RuntimeWarning displaying a 3D plot

前端 未结 1 1350
误落风尘
误落风尘 2021-01-26 09:27

I have a script which analyses a dataset and then outputs xyz data. In order to understand the distribution of the data, I want to visualize it in a 3d plot. As I have no experi

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-26 10:27

    The problem is that griddata cannot produce data for the edges of the grid. This is circumvented internally by masking the output array. However, for a masked array, a comparison xa < 0, which is needed to determine the colors, is not possible.

    The solution here would be to exclude the edges from plotting.

    ax.plot_surface(X[1:-1,1:-1], Y[1:-1,1:-1], Z[1:-1,1:-1])
    

    Complete example:

    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    import matplotlib.pyplot as plt
    from matplotlib.mlab import griddata
    import numpy as np
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    
    data = np.genfromtxt('plot.txt')
    x = data[:,0]
    y = data[:,1]
    z = data[:,2]
    
    xi = np.linspace(-1, 1)
    yi = np.linspace(-1, 1)
    
    X, Y = np.meshgrid(xi, yi)
    Z = griddata(x, y, z, xi, yi, interp='linear')
    
    surf = ax.plot_surface(X[1:-1,1:-1], Y[1:-1,1:-1], Z[1:-1,1:-1], 
                           rstride=5, cstride=5, cmap=cm.jet,
                           linewidth=1, antialiased=True)
    
    ax.set_zlim3d(np.min(Z), np.max(Z))
    
    fig.colorbar(surf)
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题