在python中绘制N个平行于XZ轴的平面

2024-04-27 03:17:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python绘制N个平行于XZ轴且彼此等距的平面(比如10个)。如果可能的话,最好从用户那里选择飞机的数量。这就像,如果用户给出“20”,那么20个平面将在3D中绘制是的。但是我想知道有没有一种方法可以调用每一个平面,或者想得到每个平面的方程??在

 import numpy as np
 import itertools
 import matplotlib.pyplot as plt
 from mpl_toolkits.mplot3d import Axes3D

 plt3d = plt.figure().gca(projection='3d')
 xx, zz = np.meshgrid(range(10), range(10))
 yy =0.5
 for _ in itertools.repeat(None, 20):

     plt3d.plot_surface(xx, yy, zz)
     plt.hold(True)
     yy=yy+.1

     plt.show()

Tags: 用户importasnp绘制rangeplt平面
1条回答
网友
1楼 · 发布于 2024-04-27 03:17:47

下面是一个如何以非常通用的方式实现所需内容的示例。在

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
from pylab import meshgrid,linspace,zeros,dot,norm,cross,vstack,array,matrix,sqrt



def rotmatrix(axis,costheta):
    """ Calculate rotation matrix

    Arguments:
    - `axis`     : Rotation axis
    - `costheta` : Rotation angle
    """
    x,y,z = axis
    c = costheta
    s = sqrt(1-c*c)
    C = 1-c
    return  matrix([[ x*x*C+c,    x*y*C-z*s,  x*z*C+y*s ],
                    [ y*x*C+z*s,  y*y*C+c,    y*z*C-x*s ],
                    [ z*x*C-y*s,  z*y*C+x*s,  z*z*C+c   ]])

def plane(Lx,Ly,Nx,Ny,n,d):
    """ Calculate points of a generic plane 

    Arguments:
    - `Lx` : Plane Length first direction
    - `Ly` : Plane Length second direction
    - `Nx` : Number of points, first direction
    - `Ny` : Number of points, second direction
    - `n`  : Plane orientation, normal vector
    - `d`  : distance from the origin
    """

    x = linspace(-Lx/2,Lx/2,Nx)
    y = linspace(-Ly/2,Ly/2,Ny)
    # Create the mesh grid, of a XY plane sitting on the orgin
    X,Y = meshgrid(x,y)
    Z   = zeros([Nx,Ny])
    n0 = array([0,0,1])

    # Rotate plane to the given normal vector
    if any(n0!=n):
        costheta = dot(n0,n)/(norm(n0)*norm(n))
        axis     = cross(n0,n)/norm(cross(n0,n))
        rotMatrix = rotmatrix(axis,costheta)
        XYZ = vstack([X.flatten(),Y.flatten(),Z.flatten()])
        X,Y,Z = array(rotMatrix*XYZ).reshape(3,Nx,Ny)

    dVec = (n/norm(n))*d
    X,Y,Z = X+dVec[0],Y+dVec[1],Z+dVec[2]
    return X,Y,Z


if __name__ == "__main__":

    # Plot as many planes as you like
    Nplanes = 10

    # Set color list from a cmap
    colorList = cm.jet(linspace(0,1,Nplanes))

    # List of Distances
    distList = linspace(-10,10,Nplanes)

    # Plane orientation - normal vector
    normalVector = array([0,1,1]) # Y direction

    # Create figure
    fig = plt.figure()
    ax = fig.gca(projection='3d')

    # Plotting
    for i,ypos in enumerate(linspace(-10,10,10)):

        # Calculate plane
        X,Y,Z = plane(20,20,100,100,normalVector,distList[i])

        ax.plot_surface(X, Y, Z, rstride=5, cstride=5,
                        alpha=0.8, color=colorList[i])

    # Set plot display parameters    
    ax.set_xlabel('X')
    ax.set_xlim(-10, 10)
    ax.set_ylabel('Y')
    ax.set_ylim(-10, 10)
    ax.set_zlabel('Z')
    ax.set_zlim(-10, 10)

    plt.show()

enter image description here

如果需要围绕法线向量旋转平面,也可以使用旋转矩阵。在

干杯

相关问题 更多 >