Plot surfaces on a cube

前端 未结 3 1228
面向向阳花
面向向阳花 2021-01-19 00:54

I would like to plot surfaces to a cube with matplotlib. I am trying to use ax.plot_surface(X, Y, Z), however I am a bit confused. What should the

3条回答
  •  孤街浪徒
    2021-01-19 01:16

    Fix for new matplotlib

    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    points = np.array([[-1, -1, -1],
                          [1, -1, -1 ],
                          [1, 1, -1],
                          [-1, 1, -1],
                          [-1, -1, 1],
                          [1, -1, 1 ],
                          [1, 1, 1],
                          [-1, 1, 1]])
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    r = [-1,1]
    X, Y = np.meshgrid(r, r)
    one = np.ones(4).reshape(2, 2)
    ax.plot_wireframe(X,Y,one, alpha=0.5)
    ax.plot_wireframe(X,Y,-one, alpha=0.5)
    ax.plot_wireframe(X,-one,Y, alpha=0.5)
    ax.plot_wireframe(X,one,Y, alpha=0.5)
    ax.plot_wireframe(one,X,Y, alpha=0.5)
    ax.plot_wireframe(-one,X,Y, alpha=0.5)
    ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.show()
    

提交回复
热议问题