88import plotly .graph_objects as go
99import seaborn as sns
1010import matplotlib .pyplot as plt
11+ import matplotlib .gridspec as gridspec
1112
1213
1314def Changeintogpkg (input_path , output_dir , layer_name ):
@@ -209,7 +210,7 @@ def count_centroids_in_contingency(raster_path):
209210 )
210211 fig .show ()
211212
212- # Seaborn for static PNG saving only
213+ # Seaborn for static PNG
213214 df_left = pd .DataFrame (
214215 {
215216 "Category" : ["Candidate" , "Benchmark" ],
@@ -224,56 +225,65 @@ def count_centroids_in_contingency(raster_path):
224225 )
225226
226227 sns .set_theme (style = "whitegrid" )
227- fig_sb , axes = plt .subplots (1 , 2 , figsize = (8 , 3 ), constrained_layout = True )
228+
229+ fig_sb = plt .figure (figsize = (10 , 3 ), constrained_layout = True )
230+ gs = gridspec .GridSpec (1 , 3 , figure = fig_sb , width_ratios = [1 , 1 , 0.4 ])
231+
232+ ax0 = fig_sb .add_subplot (gs [0 , 0 ])
233+ ax1 = fig_sb .add_subplot (gs [0 , 1 ])
234+ ax_leg = fig_sb .add_subplot (gs [0 , 2 ])
235+ ax_leg .axis ("off" )
228236
229237 def style_axes (ax , title_text , xlab , show_ylabel : bool ):
230- # Adding a bit of padding so bar labels don’t overlap with the title
231- ax .set_title (title_text , fontsize = 16 , pad = 20 )
232- ax .set_xlabel (xlab , fontsize = 14 , color = "black" )
238+ ax .set_title (title_text , fontsize = 14 , pad = 15 )
239+ ax .set_xlabel (xlab , fontsize = 13 , color = "black" )
233240 if show_ylabel :
234- ax .set_ylabel ("Flooded Building Counts" , fontsize = 14 , color = "black" )
241+ ax .set_ylabel ("Flooded Building Counts" , fontsize = 13 , color = "black" )
235242 else :
236243 ax .set_ylabel ("" )
237244
238- # Thicker black left/bottom spines
239245 for spine in ("left" , "bottom" ):
240246 ax .spines [spine ].set_linewidth (1.5 )
241247 ax .spines [spine ].set_color ("black" )
242248
243249 sns .despine (ax = ax , right = True , top = True )
244- ax .tick_params (axis = "x" , labelsize = 12 , colors = "black" )
245- ax .tick_params (axis = "y" , labelsize = 12 , colors = "black" )
250+ ax .tick_params (axis = "x" , labelsize = 11 , colors = "black" )
251+ ax .tick_params (axis = "y" , labelsize = 11 , colors = "black" )
246252
247253 # Left panel
248- ax0 = axes [0 ]
249- sns .barplot (
250- data = df_left , x = "Category" , y = "Count" , ax = ax0 , palette = ["#1c83eb" , "#a4490e" ]
251- )
252- style_axes (
253- ax0 , "Building Counts on Different FIMs" , "Inundation Surface" , show_ylabel = True
254- )
254+ colors_left = ["#1c83eb" , "#a4490e" ]
255+ sns .barplot (data = df_left , x = "Category" , y = "Count" , ax = ax0 , palette = colors_left )
256+ style_axes (ax0 , "Building Counts on Different FIMs" , "Inundation Surface" , True )
255257 for c in ax0 .containers :
256258 ax0 .bar_label (
257- c , fmt = "%.0f" , label_type = "edge" , padding = 3 , fontsize = 14 , color = "black"
259+ c , fmt = "%.0f" , label_type = "edge" , padding = 3 , fontsize = 12 , color = "black"
258260 )
259261
260262 # Right panel
261- ax1 = axes [1 ]
262- sns .barplot (
263- data = df_right ,
264- x = "Category" ,
265- y = "Count" ,
266- ax = ax1 ,
267- palette = ["#ff5733" , "#ffc300" , "#28a745" ],
268- )
269- style_axes (
270- ax1 , "Contingency Flooded Building Counts" , "Category" , show_ylabel = False
271- )
263+ colors_right = ["#ff5733" , "#ffc300" , "#28a745" ]
264+ sns .barplot (data = df_right , x = "Category" , y = "Count" , ax = ax1 , palette = colors_right )
265+ style_axes (ax1 , "Contingency Flooded Building Counts" , "Category" , False )
272266 for c in ax1 .containers :
273267 ax1 .bar_label (
274- c , fmt = "%.0f" , label_type = "edge" , padding = 3 , fontsize = 14 , color = "black"
268+ c , fmt = "%.0f" , label_type = "edge" , padding = 3 , fontsize = 12 , color = "black"
275269 )
276270
271+ # Combined legend
272+ all_labels = ["Candidate" , "Benchmark" ] + third_raster_labels
273+ all_colors = colors_left + colors_right
274+ legend_handles = [
275+ plt .Line2D (
276+ [0 ],
277+ [0 ],
278+ marker = "s" ,
279+ color = "w" ,
280+ markerfacecolor = all_colors [i ],
281+ markersize = 12 ,
282+ label = f"{ all_labels [i ]} ({ percentages [all_labels [i ]]:.2f} %)" ,
283+ )
284+ for i in range (len (all_labels ))
285+ ]
286+ ax_leg .legend (handles = legend_handles , fontsize = 12 , loc = "center left" , frameon = True )
277287 plot_dir = os .path .join (save_dir , "FinalPlots" )
278288 os .makedirs (plot_dir , exist_ok = True )
279289 output_path = os .path .join (plot_dir , f"BuildingCounts_{ basename } .png" )
0 commit comments