Plotting 3D Surface Using Python: Raise Valueerror("Argument Z Must Be 2-Dimensional.") Matplotlib [Duplicate]

Plotting 3D Surface Using Python: Raise Valueerror("Argument Z Must Be 2-Dimensional.") Matplotlib [Duplicate]

I'm trying to plot a 3d surface passes through a set of (X,Y,Z) 3d point and I got raise ValueError("Argument Z must be 2-dimensional.") matplotlib

points = Tail_geo(D,L) # is a list from a function 
points = points + Nose_geo(D,L)# is a list from both function s
X = [x[0] for x in points]# seperate X from the list
Y = [x[1] for x in points]
Z = [x[2] for x in points]
X = np.asarray(X)
Y = np.asarray(Y)
Z = np.asarray(Z)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, linewidth=0, antialiased=False)
plt . show ( )
2

1 Answer

The function plot_surface expects its inputs to be structured as a regular 2D grid. For your data (x,y,z as lists) it would probably be more appropriate to use the plot_trisurf function. Just make a simple replacement in your code.

surf = ax.plot_trisurf(X, Y, Z, linewidth=0, antialiased=False)

There is a nice example in the matplotlib gallery here which you can look through for some more details.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.