3D surface plot from a .dat file
<p>Hi I am trying to do a 3D surface plot in python by plotting from a .dat file.  I am having quite some trouble.  I finally have a code that does not produce any error messages, however the plot displays nothing, even though the .dat file is not empty.  My code is below. </p>

<p>How can I plot my data from a .dat file via a 3Dsurface plot?  My data is formatted in the .dat file as three columns. Thanks for the help!</p>

<p>Update:  The .dat file is not empty, I am able to plot the same .dat file with a 3D scatter plot fine, but it  will not work in this 3D surface plot code.  The .dat file looks something like this (just 3 columns of numbers):</p>

<pre><code>1 2 6
3 8 7
4 9 3
5 2 5


from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

data = np.genfromtxt('file.dat')
x = data[:,0]
y = data[:,1]
z = data[:,2]

xi = np.linspace(min(x), max(x),0.1)
yi = np.linspace(min(y), max(y),0.1)

X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi, interp='linear')

ax.set_xlabel('$x$', fontsize = 14)
ax.set_ylabel('$y$', fontsize = 14)
ax.set_zlabel('$z$', fontsize = 14)
ax.set_title('result..', fontsize = 14)

surf = ax.plot_surface(X, Y, Z, linewidth=1, antialiased=True)

ax.set_zlim3d(0,1)

plt.show()
</code></pre>

<p>My guess is that your problem comes down to a rather trivial mistake you're making with <code>np.linspace</code> that I didn't notice when I commented. The third argument of <code>np.linspace</code> is the number of points in the array, not the spacing between points (that would be <code>np.arange</code>).</p>

<p>In case I'm wrong, I'll show a full working example here (modified from <a href="http://matplotlib.org/examples/pylab_examples/griddata_demo.html" rel="nofollow">http://matplotlib.org/examples/pylab_examples/griddata_demo.html</a>), and if your surface plot still doesn't show up when running this example, then it may be a more basic issue you're having with <code>matplotlib</code>.</p>

<pre><code>from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

#data = np.genfromtxt('file.dat')
npts = 1000
x = np.random.uniform(-2, 2, npts)
y = np.random.uniform(-2, 2, npts)
z = x*np.exp(-x**2 - y**2)

xi = np.linspace(min(x), max(x), 100) # &lt;-- note change to 100
yi = np.linspace(min(y), max(y), 100) # &lt;-- note change to 100

X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi, interp='linear')

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

ax.set_zlim3d(np.min(Z), np.max(Z))
fig.colorbar(surf)
</code></pre>

<p><a href="https://i.stack.imgur.com/cWm5E.png" rel="nofollow"><img src="https://i.stack.imgur.com/cWm5E.png" alt="enter image description here"></a></p>

<p>Note that your use of <code>ax.set_zlim3d(0,1)</code> could also be an issue. I wouldn't recommend setting limits on your axes before you see your data plotted.</p>

python matplotlib plot