Plotting a 2d Array with mplot3d

前端 未结 4 1920
一整个雨季
一整个雨季 2021-01-31 20:40

I have a 2D numpy array and I want to plot it in 3D. I heard about mplot3d but I cant get to work properly

Here\'s an example of what I want to do. I have an array with

4条回答
  •  南旧
    南旧 (楼主)
    2021-01-31 20:58

    It sounds like you are trying to create a surface plot (alternatively you could draw a wireframe plot or a filled countour plot.

    From the information in the question, you could try something along the lines of:

    import numpy
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    # Set up grid and test data
    nx, ny = 256, 1024
    x = range(nx)
    y = range(ny)
    
    data = numpy.random.random((nx, ny))
    
    hf = plt.figure()
    ha = hf.add_subplot(111, projection='3d')
    
    X, Y = numpy.meshgrid(x, y)  # `plot_surface` expects `x` and `y` data to be 2D
    ha.plot_surface(X, Y, data)
    
    plt.show()
    

    Obviously you need to choose more sensible data than using numpy.random in order to get a reasonable surface.

提交回复
热议问题