matplotlib线框图/ 3d绘图howTo

时间:2012-08-01 15:22:19

标签: python 3d plot matplotlib wireframe

我想要一个带有matplotlib的3d图。

数据如下:我有一个矩阵,每行包含3d绘图的Y坐标。每行第一个元素是3d图的X坐标。最后,第二个矩阵在X,Y位置为每个点包含高。因此,该第二矩阵包含我的Z坐标。两个矩阵都是使用Python的数组数组。我想知道如何转换数据以获得:

  • 与X对应的每个1d信号的图表,如下所示(图片可在线获取)enter image description here
  • 相同数据的线框图,例如enter image description here

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

 ########  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,Y,Z的2D列表。

对于第一张图,我阅读了帮助,并希望在3d中使用2d图。示例源代码给出:

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是常量坐标。我适应

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

但有警告:non-keyword arg after keyword arg。怎么解决?

谢谢和问候

1 个答案:

答案 0 :(得分:0)

关于在3D中显示一系列矢量,我得到了以下“几乎正常工作”的解决方案:

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

对于2D曲面或网格图,我使用了meshgrid。请注意,一旦知道如何构建网格网格,就可以自己重现网格网格。有关meshgrid的更多信息,请参阅this post

这是代码(因为它引用了类成员,所以不能使用它,但你可以根据我正在使用的matplotlib的3d plot方法构建你的代码)

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