matplotlib 3d surface plots not showing

Labibah picture Labibah · Apr 9, 2015 · Viewed 11.8k times · Source

I am trying to make a simple 3D surface plot with matplotlib but the plot does not show at the end; I only get empty 3D axes.

Here is what I did:

from mpl_toolkits.mplot3d import Axes3D

x = np.arange(1, 100, 1)
y = np.arange(1, 100, 1)
z = np.arange(1, 100, 1)

fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=5, cstride=5) 
show()

...and I get this: enter image description here

Any suggestions?

Answer

Kirubaharan J picture Kirubaharan J · Apr 10, 2015
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
from scipy.interpolate import griddata

x= np.arange(1, 100, 1)
y= np.arange(1, 100, 1)
z= np.arange(1, 100, 1)
fig = plt.figure()
ax = fig.gca(projection='3d')
xi = np.linspace(x.min(), x.max(), 50)
yi = np.linspace(y.min(), y.max(), 50)
zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='nearest')    # create a uniform spaced grid
xig, yig = np.meshgrid(xi, yi)
surf = ax.plot_wireframe(X=xig, Y=yig, Z=zi, rstride=5, cstride=3, linewidth=1)   # 3d plot
plt.show()

enter image description here