Plotting a 2d Array with mplot3d

前端 未结 4 1836
一整个雨季
一整个雨季 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:53

    You can also use oct2py module which is actually an python-octave bridge. Using it you can exploit fucntions of octave, and you can get the thing you need, and it's pretty easy as well.

    check out this documentation : https://www.gnu.org/software/octave/doc/v4.0.1/Three_002dDimensional-Plots.html

    And for sample example:

    from oct2py import octave as oc
    
    tx = ty = oc.linspace (-8, 8, 41)
    [xx, yy] = oc.meshgrid (tx, ty)
    r = oc.sqrt (xx * xx + yy * yy) + oc.eps()
    tz = oc.sin (r) / r
    oc.mesh (tx, ty, tz)
    

    Above is the python code, which is as same as the first example implemented in octave in the above documentation.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-31 21:02

    You can try a 3D bar plot using function bar3d.

    Suppose you have an array A of dimension (25, 10), the value with the index (i, j) is A[i][j]. The following code sample can give you a 3D bar plot, where the height of each bar is A[i][j].

    from mpl_toolkits.mplot3d import axes3d
    import matplotlib.pyplot as plt
    import numpy as np
    %matplotlib inline
    
    np.random.seed(1234)
    fig = plt.figure()
    ax1 = fig.add_subplot(111, projection='3d')
    A = np.random.randint(5, size=(25, 10))
    
    x = np.array([[i] * 10 for i in range(25)]).ravel() # x coordinates of each bar
    y = np.array([i for i in range(10)] * 25) # y coordinates of each bar
    z = np.zeros(25*10) # z coordinates of each bar
    dx = np.ones(25*10) # length along x-axis of each bar
    dy = np.ones(25*10) # length along y-axis of each bar
    dz = A.ravel() # length along z-axis of each bar (height)
    
    ax1.bar3d(x, y, z, dx, dy, dz)
    

    On my PC with random seed 1234, I get the following plot:

    However, it might be slow to make the plot for your problem with dimension (256, 1024).

    0 讨论(0)
  • 2021-01-31 21:02

    You can find the answer in one of the examples of the Matplotlib gallery; the 3D examples are towards the end.

    More generally, the Matplotlib gallery is a great first-stop resource, for finding how to do some plots.

    The examples I looked at essentially work with three 2D arrays: one with all the x values, one with all the y values, and the last one with all the z values. So, one solution is to create the arrays of x and y values (with meshgrid(), for instance).

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