4

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
HarleyH
  • 59
  • 1
  • 6

1 Answers1

2

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

Serenity
  • 35,289
  • 20
  • 120
  • 115
  • Does Z need to be a function of X and Y like in the example? My Z value is from a data set so I can't write a function for it – HarleyH Jun 10 '16 at 23:42
  • No, in my example `Z` is a 2d array as the same shape as X and Y. You may write like `z[0,0]=.5` etc. – Serenity Jun 11 '16 at 00:04
  • Thanks Stanley I got it to plot in 3d! The problem I'm having now is the plot is 4 or 5 separate planes with a plane on the xy axis. I expected this because I made the Z array with all zero values besides the ones that are modified by the coordinates X, Y. Is there a way to interpolate values of some sort? I tried the trisurf method but the library wasn't loading correctly. I will post a picture tomorrow morning when I get my computer charger if what I'm saying is hard to visualize. – HarleyH Jun 11 '16 at 04:19