Friday, January 15, 2016
Plot 3D
#!/usr/bin/python
from
mpl_toolkits.mplot3d import Axes3D
import numpy as np
import scipy
from
scipy.interpolate import griddata
import
matplotlib.pyplot as plt
import
matplotlib.mlab as ml
import scipy.ndimage
as ndimage
from matplotlib
import mlab, cm
def
extrapolate_nans(x, y, v):
if
np.ma.is_masked(v):
nans =
v.mask
else:
nans =
np.isnan(v)
notnans =
np.logical_not(nans)
v[nans] =
scipy.interpolate.griddata((x[notnans], y[notnans]), v[notnans],
(x[nans],
y[nans]), method='nearest').ravel()
return v
data =
np.genfromtxt('input2d.txt')
x1 = data[:,0]
y1 = data[:,1]
z1 = data[:,2]
numcols, numrows =
500, 500
xi =
np.linspace(min(x1), max(x1), numcols)
yi =
np.linspace(min(y1), max(y1), numrows)
xi, yi =
np.meshgrid(xi, yi)
x, y, z = x1, y1, z1
zi = ml.griddata(x,
y, z, xi, yi, interp='nn')
extrapolate_nans(xi,yi,zi)
levels =
np.arange(-1200, -200, 25) # Boost the upper limit to avoid
truncation errors.
norm =
cm.colors.Normalize(vmax=abs(z1).max(), vmin=-abs(z1).max())
cmap = cm.hsv
####plot
zis =
ndimage.gaussian_filter(zi, sigma=25.0, order=0)
fig=plt.figure()
ax =
fig.gca(projection='3d')
surf =
ax.plot_surface(xi, yi, zis, rstride=20, cstride=20,
cmap=cm.get_cmap(cmap, len(levels) - 1), linewidth=0,
antialiased=True)
ax.set_zlim3d(np.min(zis),
np.max(zis))
fig.colorbar(surf)
plt.show()
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment