matplotlib - 3d surface from a rectangular array of heights

后端 未结 1 1932
感动是毒
感动是毒 2020-12-15 07:16

I am trying to plot some HDF data in matplotlib. After importing them using h5py, the data is stored in a form of array, like this:

array([[151, 176, 178],
          


        
相关标签:
1条回答
  • 2020-12-15 07:33

    If you want a 3-d surface plot, you have to create the meshgrid first. You can try:

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    
    X = np.arange(1, 10)
    Y = np.arange(1, 10)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot', linewidth=0, antialiased=False)
    ax.set_zlim(-1.01, 1.01)
    
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.show()
    

    which will generate, enter image description here

    However, if the only relevant information is in the z-values, you can simply use imshow. Here, z-values are represented by their color. You can achieve this by:

    im = plt.imshow(Z, cmap='hot')
    plt.colorbar(im, orientation='horizontal')
    plt.show()
    

    Which will give,

    enter image description here

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