2012-08-01 57 views
2

我想用matplotlib做3d圖。matplotlib線框圖/三維圖如何

數據如下:我有一個矩陣,每行包含Y座標的3d圖。每行第一個元素是3d圖的X座標。最後,第二個矩陣在X,Y位置包含每個點的高位。這第二個矩陣包含了我的Z座標。兩個矩陣都是使用Python的數組的數組。我想知道如何轉換數據,以便獲得:

  • 有與X,像這樣(圖片可在線)各一維信號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。怎麼修?

感謝和問候

+0

你的意思是一個例外。嘗試用'ax.plot(xs = x,ys-y,zs = z,xdir ='x')改變'ax.plot(xs = x,y,z,xdir ='x')' – pelson 2012-08-01 17:27:34

回答

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。請注意,一旦您知道如何構建meshgrid,您就可以自行重現meshgrid。有關meshgrid的更多信息,請參閱this post

下面是代碼(因爲它是指類的成員不能使用它是這樣,但你可以根據從matplotlib 3D繪圖方法,我使用生成代碼)

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