Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中以3D方式绘制平行于XZ轴的N个平面_Python_Python 2.7_Numpy_Matplotlib - Fatal编程技术网

在python中以3D方式绘制平行于XZ轴的N个平面

在python中以3D方式绘制平行于XZ轴的N个平面,python,python-2.7,numpy,matplotlib,Python,Python 2.7,Numpy,Matplotlib,我想使用python绘制与XZ轴平行且彼此等距的N个平面(比如10个)。如果可能的话,最好从用户那里选择飞机的数量。这就像,如果用户给出“20”,那么将在3D中绘制20个平面。这就是我所做的。但是我想知道有没有一种方法可以调用每个平面或者得到每个平面的方程 import numpy as np import itertools import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D plt3d =

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

 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()

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

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()

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


干杯

您的代码在哪里?:)我使用itertools生成了N次曲面,每次y值都会改变。是否可以分别得到这些平面的方程?现在您有了一个计算通用平面点的函数。您甚至可以使用任何您喜欢的平面方向。如果你想得到平面的方程式,你可以使用平面的点,并使用维基百科页面中描述的概念,