matplotlib线框剧情/ 3D绘图HOWTO [英] matplotlib wireframe plot / 3d plot howTo

查看:376
本文介绍了matplotlib线框剧情/ 3D绘图HOWTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个3D绘图与matplotlib。

I would like to have a 3d plot with matplotlib.

数据如下:我与含Y的每一行的矩阵坐标的三维图。每一行第一个元件是在X坐标的三维图。最后,第二基质中含有较高的每一个点,在X,Y位置。这第二个矩阵因此包含我的Z轴坐标。两个矩阵是与Python数组的数组。我想知道如何转换数据,以便获得:

Data are the following: I have a matrix with each row containing Y coordinates for the 3d plot. Each row first elements are the X coordinates for the 3d plot. Finally, a second matrix contains high for each point, at a X,Y position. This second matrix thus contains my Z coordinates. Both matrices are arrays of arrays with Python. I would like to know how to transform data so as to obtain:

  • 对应一个X每1D信号的情节,这(照片在网上提供),如
  • 线框剧情为相同的数据,这样的

我写了一个辅助函数的线框工作,

I have written an helper function for a wireframe work,

 ########  HELPER FOR PLOT 3-D

 def plot_3d(name,X,Y,Z):

     fig = plt.figure(name)
     ax = fig.gca(projection='3d')
     X = np.array(X)
     Y = np.array(Y)
     Z = np.array(Z)
     ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10)
     ax.set_xlabel('X Label')
     ax.set_ylabel('Y Label')
     plt.show()

但我不知道如何将数据X,Y,Z,使他们matplotlib功能的配合要求,希望对于x 2D名单,Y,Z。

but I dont know how to transform data X,Y,Z to make them fit requirements for matplotlib function, which want 2D lists for X, Y ,Z.

有关第一张图,我读的帮助,并希望使用2D情节的3D。示例源$ C ​​$ C给出:

For first graph, I read help, and want to use 2d plot in 3d. Example source code gives:

x = np.linspace(0, 1, 100)
y = np.sin(x * 2 * np.pi) / 2 + 0.5
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')

其中z是常数坐标。就我而言,x是恒定的坐标。我和适应

where z is the constant coordinate. In my case, x is the constant coordinate. I adapt with

        fig = plt.figure('2d profiles')
        ax = fig.gca(projection='3d')
        for i in range(10):
             x = pt  ## this is a scalar
             y = np.array(y)
             z = np.array(z)
             ax.plot(xs = x, y, z, xdir='x')
        plt.show()

但有警告:非关键字ARG后关键字ARG 。如何解决?

感谢和问候

推荐答案

对于三维向量的意甲的显示,我带着下面的几乎工作的解决方案:

Regarding the display of a serie of vectors in 3D, I came with following 'almost working' solution:

def visualizeSignals(self, imin, imax):

    times = self.time[imin:imax]
    nrows = (int)((times[(len(times)-1)] - times[0])/self.mod) + 1

    fig = plt.figure('2d profiles')
    ax = fig.gca(projection='3d')
    for i in range(nrows-1):
        x = self.mat1[i][0] + self.mod * i
        y = np.array(self.mat1T[i])
        z = np.array(self.mat2[i])
        ax.plot(y, z, zs = x, zdir='z')

    plt.show()

对于二维表面或meshgrid情节,我通过使用meshgrid。需要注意的是,你可以通过自己一旦你知道如何meshgrid是建立再现meshgrid。有关meshgrid更多的信息,我指的是这个帖子

下面是code(因为它指向类成员不能使用它作为这样的,但你可以根据从matplotlib我使用的3D绘图方式建立你的code)

Here is the code (cannot use it as such since it refers to class members, but you can build your code based on 3d plot methods from matplotlib I am using)

def visualize(self, imin, imax, typ_ = "wireframe"):
    """
    3d plot signal between imin and imax
    . typ_: type of plot, "wireframce", "surface"
    """

    times = self.retT[imin:imax]
    nrows = (int)((times[(len(times)-1)] - times[0])/self.mod) + 1

    self.modulate(imin, imax)

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

    x = []
    for i in range(nrows):
        x.append(self.matRetT[i][0] + self.mod * i)

    y = []
    for i in range(len(self.matRetT[0])):
        y.append(self.matRetT[0][i])
    y = y[:-1]


    X,Y = np.meshgrid(x,y)

    z = [tuple(self.matGC2D[i]) for i in range(len(self.matGC))] # matGC a matrix

    zzip = zip(*z)

    for i in range(len(z)):
        print len(z[i])

    if(typ_ == "wireframe"):
        ax.plot_wireframe(X,Y,zzip)
        plt.show()
    elif(typ_ == "contour"):
        cset = ax.contour(X, Y, zzip, zdir='z', offset=0)
        plt.show()
    elif(typ_ == "surf_contours"):
        surf = ax.plot_surface(X, Y, zzip, rstride=1, cstride=1, alpha=0.3)
        cset = ax.contour(X, Y, zzip, zdir='z', offset=-40)
        cset = ax.contour(X, Y, zzip, zdir='x', offset=-40)
        cset = ax.contour(X, Y, zzip, zdir='y', offset=-40)
        plt.show()

这篇关于matplotlib线框剧情/ 3D绘图HOWTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆