The following example is from Chuck Swanson, a previous student in GSP 318. The code requires the numpy, scipy, and matplotlib packages. It also uses mpl_toolkits which is in matplotlib.
#--------------------------------------------------------------------------------------------------------- # 3D plotting tool to plot water surface elevation data output by EFDC Explorer 7.1 # Hydrodynamic modeling software. Data being plotted is exported as "wselev.plt" # the input file must have this name to be compatible with this program # # Author: Chuck Swanson # final project for GSP 118 Spring 2014 # # Required Packages: # matplotlib # scipy # # Required Inputs: # "wselev.plt" water surface elevation file from EFDC Explorer 7.1 # file is formatted as a tecplot file # # Outputs: # 3D surface plot # #---------------------------------------------------------------------------------------------------------- from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np import Tkinter, tkFileDialog, tkMessageBox from mpl_toolkits.mplot3d import Axes3D from scipy.interpolate import griddata try: # Get EFDC Explorer 7.1 output file from user and open a new output file in the same directory root = Tkinter.Tk() # create Tkinter window root.withdraw() # close Tkinter window InputFilePath = tkFileDialog.askopenfilename() # use Windows Explorer to select file if not InputFilePath.endswith("wselev.plt"): raise Exception # check if input file selected is the correct file, otherwise abort except Exception: tkMessageBox.showerror("Incorrect File","I'm sorry, this module is only compatible") quit() #------------------------------------------------------------------------------------------------- # The following code adapted from: # http://stackoverflow.com/questions/12730436/matplotlib-plotting-non-uniform-data-in-3d-surface # open input file and use numpy implicit function to read data to x, y, and z 1D arrays with open(InputFilePath,"r") as InputFile: my_data = np.genfromtxt(InputFile, delimiter=' ',skiprows=3) X = my_data[:,0] Y = my_data[:,1] Z = my_data[:,2] # create figure object and assign visual aspect fig = plt.figure(figsize=plt.figaspect(0.5)) ax = fig.add_subplot(1, 2, 1, projection='3d') # assign grid arrays for plotting and interpolate z-data using griddata function # griddata methods: # "nearest" next nearest neighbor interpolation applies z values at grid location from # nearest data value # "cubic" uses cubic spline interpolation # 'linear" uses linear interpolation (only used for regularly spaced data) xi = np.linspace(X.min(),X.max(),100) yi = np.linspace(Y.min(),Y.max(),100) zi = griddata((X, Y), Z, (xi[None,:], yi[:,None]), method='nearest') # create 3D contour mesh plot CS = plt.contour(xi,yi,zi,15,linewidths=0.5,color='k') ax = fig.add_subplot(1, 2, 2, projection='3d') xig, yig = np.meshgrid(xi, yi) # create 3D surface plot surf = ax.plot_surface(xig, yig, zi, linewidth=0) # open plotting window for viewing plt.show()
© Copyright 2018 HSU - All rights reserved.