HarleyH
HarleyH

Reputation: 59

How to create a 3d surface plot

I have referred to matplotlib.org and a lot of threads but I can't plot a 3d surface. I am able to plot a 3d scatter but as soon as I try to plot a 3d surface it breaks for a lot of different errors. I fix errors but I get new ones. After fighting with it for a 2 days I believe my understanding of matplotlib 3d surfaces is wrong.

I want to plot SA,FZ, FY

where SA is X, FZ is Y, FY is Z

 while y<Last_Row:
    if 100 < Temp_AVG_Dict[y] and Temp_AVG_Dict[y] < 140 and abs(Camber[y])<5 and 24.5<Speed[y] and Speed[y]<25.5:
        y = y + 1

        SA[SArow:]=[Run_50_sheet.cell_value( y , SA_Column)]
        SArow = SArow + 1

        FY[FYrow:] = [Run_50_sheet.cell_value( y , FY_Column)]
        FYrow = FYrow + 1

        FZ[FZrow:] = [Run_50_sheet.cell_value( y , FZ_Column)]
        FZrow = FZrow + 1

        Z.insert(SArow,[SA[SArow-1], FZ[FZrow-1], FY[FYrow-1]])

    else:
        y=y+1

Z= np.array(Z)


fig = plt.figure(1)
ax = fig.add_subplot(111, projection='3d')

This line below is where I believe I am going wrong. X,Y,Z need to all be 2d arrays but im unsure of the values that need to be input.

I tried making 2d arrays in the order of [SA,FZ], [FZ,FY], and [SA,FY] but that failed I also tried making SA and FY equal to 1..n but that didn't work either.

ax.plot_surface( SA, FY ,Z)
plt.show()

What values should the 2d arrays be made of? Any help is appreciated.

Upvotes: 4

Views: 1104

Answers (1)

Serenity
Serenity

Reputation: 36635

Look at this example code for 3d surface: http://matplotlib.org/examples/mplot3d/surface3d_demo.html

First, you have to create two regular mesh grids X and Y (2d arrays) to plot with plot_surface, like here:

X = np.arange(-5, 5, 0.25) # X, Y are 1d arrays
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)   # Now X, Y are 2d arrays

Try to make mesh grids of your one-dimensional arrays FA (if it is x), FY (if it is y):

X, Y = np.meshgrid(FA, FY)

Then, create 2d array Z which represents values of the surface, i.e.

Z = np.sin(np.sqrt(X**2 + Y**2))

Your FZ array has to be 2d and has the same shape as X, Y arrays.

After plot:

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

enter image description here

If you mesh grids are not regular, look for plot_trisurf: http://matplotlib.org/examples/mplot3d/trisurf3d_demo.html

Upvotes: 3

Related Questions