Question

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

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:

  • a plot of each 1d signal corresponding to an X, like this (photo available online) enter image description here
  • a wireframe plot for same data, like this enter image description here

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

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.

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

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

but there is warning: non-keyword arg after keyword arg. How to fix?

Thanks and regards

Was it helpful?

Solution

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

As for 2D surface or meshgrid plot, I come through using meshgrid. Note that you can reproduce a meshgrid by yourself once you know how a meshgrid is built. For more info on meshgrid, I refer to this post.

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top