@@ -1967,7 +1967,7 @@ def updatePositions(self):
19671967 platform .r [1 ] = platform .body .r6 [1 ]
19681968
19691969
1970- def plot2d (self , ax = None , plot_seabed = True ,plot_bathymetry = True , plot_boundary = True , bare = False , axis_equal = True ,save = False ,** kwargs ):
1970+ def plot2d (self , ax = None , plot_seabed = False , draw_soil = False ,plot_bathymetry = True , plot_boundary = True , bare = False , axis_equal = True ,save = False ,** kwargs ):
19711971 '''Plot aspects of the Project object in matplotlib in 3D.
19721972
19731973 TODO - harmonize a lot of the seabed stuff with MoorPy System.plot...
@@ -1991,6 +1991,9 @@ def plot2d(self, ax=None, plot_seabed=True,plot_bathymetry=True, plot_boundary=T
19911991 plot_moorings = kwargs .get ('plot_moorings' ,True )
19921992 plot_cables = kwargs .get ('plot_cables' ,True )
19931993 cable_labels = kwargs .get ('cable_labels' , False )
1994+ depth_vmin = kwargs .get ('depth_vmin' , None )
1995+ depth_vmax = kwargs .get ('depth_vmax' , None )
1996+ bath_levels = kwargs .get ('bath_levels' , None )
19941997
19951998
19961999 # if axes not passed in, make a new figure
@@ -2002,21 +2005,41 @@ def plot2d(self, ax=None, plot_seabed=True,plot_bathymetry=True, plot_boundary=T
20022005
20032006 # Bathymetry
20042007 if plot_bathymetry :
2008+ if plot_seabed :
2009+ raise ValueError ('The bathymetry grid and soil grid cannot yet be plotted at the same time' )
20052010 if len (self .grid_x ) > 1 and len (self .grid_y ) > 1 :
2006- num_levels = 100 # Adjust this value as needed
2007- X , Y = np .meshgrid (self .grid_x , self .grid_y )
20082011
2009- contourf = ax .contourf (X , Y , self .grid_depth , num_levels , cmap = 'Blues' , vmin = np .min (self .grid_depth ), vmax = np .max (self .grid_depth ))
2010- #contourf = ax.contourf(X, Y, self.grid_depth, num_levels, cmap='Blues', vmin=500, vmax=1300)
2011- # >>>> TODO: Update the above to add optional inputs for bounds (vmin/vmax) on contour plot colors for bathymetry <<<<
2012-
2012+ X , Y = np .meshgrid (self .grid_x , self .grid_y )
2013+
2014+ num_levels = bath_levels if bath_levels is not None else 50
2015+ vmin = depth_vmin if depth_vmin is not None else np .min (self .grid_depth )
2016+ vmax = depth_vmax if depth_vmax is not None else np .max (self .grid_depth )
2017+ grid_depth = np .clip (self .grid_depth , vmin , vmax )
2018+
2019+ contourf = ax .contourf (X , Y , grid_depth , num_levels , cmap = 'Blues' , vmin = np .min (self .grid_depth ), vmax = np .max (self .grid_depth ))
2020+
2021+ contourf .set_clim (depth_vmin , depth_vmax )
2022+
20132023 if not bare : # Add colorbar with label
20142024 import matplotlib .ticker as tkr
20152025 cbar = plt .colorbar (contourf , ax = ax , fraction = 0.04 , label = 'Water Depth (m)' , format = tkr .FormatStrFormatter ('%.0f' ))
2016- # if plot_seabed:
2017- # if len(self.soil_x) > 1 and len(self.soil_y) > 1:
2018- # sX, sY = np.meshgrid(self.soil_x, self.soil_y)
2019- # ax.scatter(sX, sY, self.soil_names)
2026+
2027+
2028+ if plot_seabed :
2029+ if plot_bathymetry :
2030+ raise ValueError ('The bathymetry grid and soil grid cannot yet be plotted at the same time' )
2031+ import matplotlib .colors as mcolors
2032+ soil_types = np .unique (self .soil_names )
2033+ soil_type_to_int = {name : i for i ,name in enumerate (soil_types )}
2034+ soil_colors = {'mud' :'green' , 'hard' :'brown' }
2035+ soil_int = np .vectorize (soil_type_to_int .get )(self .soil_names )
2036+ cmap = mcolors .ListedColormap ([soil_colors .get (name , 'white' ) for name in soil_types ])
2037+
2038+ X , Y = np .meshgrid (self .soil_x , self .soil_y )
2039+ ax .pcolormesh (X , Y , soil_int , cmap = cmap , shading = 'auto' )
2040+
2041+ soil_handles = [plt .Line2D ([0 ], [0 ], marker = 's' , color = 'w' , label = name , markerfacecolor = soil_colors .get (name , 'white' ), markersize = 10 ) for name in soil_types if name != '0' ]
2042+
20202043
20212044 if plot_boundary :
20222045 if len (self .boundary ) > 1 :
@@ -2160,6 +2183,9 @@ def plot2d(self, ax=None, plot_seabed=True,plot_bathymetry=True, plot_boundary=T
21602183 ax .set_aspect ('equal' ,adjustable = 'box' )
21612184
21622185 handles , labels = plt .gca ().get_legend_handles_labels ()
2186+ if plot_seabed :
2187+ handles += soil_handles
2188+ labels += [h .get_label () for h in soil_handles ]
21632189 by_label = dict (zip (labels , handles )) # Removing duplicate labels
21642190 ax .legend (by_label .values (), by_label .keys (),loc = 'upper center' ,bbox_to_anchor = (0.5 , - 0.1 ), fancybox = True , ncol = 4 )
21652191 if save :
0 commit comments