Matplotlib 3D Surface missing 1 required positional argument: 'Z'

Viewed 7631

I'm trying to make 3D surface just like in this example: https://gis.stackexchange.com/questions/66367/display-a-georeferenced-dem-surface-in-3d-matplotlib

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import *
from matplotlib.mlab import griddata
from matplotlib import cm

data = np.random.random((20, 2))
z = np.random.randint(5, 30, 20)
x = data.T[0]
y = data.T[-1]
xi = np.linspace(min(x), max(x))
yi = np.linspace(min(x), max(y))
X, Y = np.meshgrid(xi, yi)

Z = griddata(x, y, z, xi, yi)

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter3D(x, y, z, c=z)
Axes3D.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=1, antialiased=True)
plt.show()

I think my code is similar as the example, but it doesn't work. I got this error

TypeError: plot_surface() missing 1 required positional argument: 'Z'

How can I fix it?

1 Answers

You just need to use your 3D axis object to plot. To do so, replace

Axes3D.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=1, antialiased=True) 

by

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=1, antialiased=True) 

I am not even sure why you were in the first place using Axes3D.plot_surface even after having defining an object ax of kind Axes3D.

P.S: I (using matplotlib version 2.2.2) also got a warning

The griddata function was deprecated in version 2.2.

enter image description here

Related