Plotting a 2d Array with mplot3d

前端 未结 4 1919
一整个雨季
一整个雨季 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 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).

提交回复
热议问题