2012-08-01 6 views
2

matplotlib로 3d 플롯을 만들고 싶습니다.matplotlib 와이어 프레임 플롯/3d 플롯 howTo

데이터는 다음과 같습니다. 각 행에 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() 

하지만, Y를 데이터 X를 변환하는 방법을 잘 모릅니다, Z를 사용하여 X, Y, Z에 대한 2D 목록을 원하는 matplotlib 함수에 대한 요구 사항에 맞 춥니 다.

첫 번째 그래프의 경우 도움말을 읽고 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가있다. 어떻게 고치는 지?

감사 및 3D에서 벡터의 세리의 표시에 관한 안부

+0

당신은 예외를 의미한다. 'ax.plot (xs = x, ys-y, zs = z, xdir ='x ')'로'ax.plot (xs = x, y, z, xdir ='x ' – pelson

답변

0

, 내가 솔루션 '거의 작업'다음과 함께 : 2D 표면 또는 meshgrid 음모에 관해서는

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에 대한 자세한 내용은 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() 
관련 문제