@@ -136,18 +136,17 @@ def fig_04c(chreef_data, save_path, plot=False, plot_by_side=False, use_alias=Tr
136136
137137 main_label_size = 20
138138 sub_label_size = 16
139- main_tick_size = 12
139+ main_tick_size = 16
140140 legendsize = 16
141141
142142 if plot_by_side :
143- plt .scatter (x , sgns_left , label = "SGN count ( Left) " , marker = "o" , s = 80 )
144- plt .scatter (x , sgns_right , label = "SGN count ( Right) " , marker = "x" , s = 80 )
143+ plt .scatter (x , sgns_left , label = "Left" , marker = "o" , s = 80 )
144+ plt .scatter (x , sgns_right , label = "Right" , marker = "x" , s = 80 )
145145 else :
146146 plt .scatter (x , sgns , label = "SGN count" , marker = "o" , s = 80 )
147147
148148 # Labels and formatting
149149 plt .xticks (x , alias , fontsize = sub_label_size )
150- plt .xlabel ("Cochlea" , fontsize = main_label_size )
151150 plt .yticks (fontsize = main_tick_size )
152151 plt .ylabel ("SGN count per cochlea" , fontsize = main_label_size )
153152 plt .ylim (4000 , 13800 )
@@ -224,7 +223,7 @@ def fig_04d(chreef_data, save_path, plot=False, plot_by_side=False, intensity=Fa
224223
225224 main_label_size = 20
226225 sub_label_size = 16
227- main_tick_size = 12
226+ main_tick_size = 16
228227 legendsize = 16
229228
230229 label = "Intensity" if intensity else "Transduction efficiency"
@@ -237,7 +236,6 @@ def fig_04d(chreef_data, save_path, plot=False, plot_by_side=False, intensity=Fa
237236
238237 # Labels and formatting
239238 plt .xticks (x , alias , fontsize = sub_label_size )
240- plt .xlabel ("Cochlea" , fontsize = main_label_size )
241239 plt .yticks (fontsize = main_tick_size )
242240 plt .ylabel (label , fontsize = main_label_size )
243241 plt .legend (loc = "best" , fontsize = sub_label_size )
@@ -262,8 +260,7 @@ def fig_04d(chreef_data, save_path, plot=False, plot_by_side=False, intensity=Fa
262260 plt .close ()
263261
264262
265- def fig_04e (chreef_data , save_path , plot , intensity = False , gerbil = False , use_alias = True , trendlines = False ,
266- trendline_fit = "linear_regression" ):
263+ def fig_04e (chreef_data , save_path , plot , intensity = False , gerbil = False , use_alias = True , trendlines = False ):
267264
268265 result = {"cochlea" : [], "octave_band" : [], "value" : []}
269266 for name , values in chreef_data .items ():
@@ -312,6 +309,8 @@ def fig_04e(chreef_data, save_path, plot, intensity=False, gerbil=False, use_ali
312309 cochleas = sorted ({name_lr [:- 1 ] for name_lr in result ["cochlea" ].unique ()})
313310 colors = plt .cm .tab10 .colors # pick a colormap
314311 color_map = {cochlea : colors [i % len (colors )] for i , cochlea in enumerate (cochleas )}
312+ if len (cochleas ) == 1 :
313+ color_map = {"L" : colors [0 ], "R" : colors [1 ]}
315314
316315 # Track which cochlea names we have already added to the legend
317316 legend_added = set ()
@@ -323,16 +322,24 @@ def fig_04e(chreef_data, save_path, plot, intensity=False, gerbil=False, use_ali
323322
324323 for name_lr , grp in result .groupby ("cochlea" ):
325324 name , side = name_lr [:- 1 ], name_lr [- 1 ]
325+ if len (cochleas ) == 1 :
326+ label_name = name_lr
327+ color = color_map [side ]
328+ else :
329+ label_name = name
330+ color = color_map [name ]
331+
326332 x_positions = grp ["x_pos" ] + offset_map [side ]
327333 ax .scatter (
328334 x_positions ,
329335 grp ["value" ],
330- label = name if name not in legend_added else None ,
336+ label = label_name if label_name not in legend_added else None ,
331337 s = 60 ,
332338 alpha = 0.8 ,
333339 marker = "o" if side == "L" else "x" ,
334- color = color_map [ name ]
340+ color = color ,
335341 )
342+
336343 if name not in legend_added :
337344 legend_added .add (name )
338345
@@ -361,43 +368,32 @@ def get_trendline_values(trend_dict, side):
361368 # Trendline left
362369 x_sorted , y_sorted = get_trendline_values (trend_dict , "L" )
363370
364- if trendline_fit == "linear_regression" :
365- # linear regression
366- coeffs = np .polyfit (x_sorted , y_sorted , 1 )
367- poly_fn = np .poly1d (coeffs )
368-
369- ax .plot (
370- x_sorted ,
371- poly_fn (x_sorted ),
372- linestyle = "dotted" ,
373- color = "red" ,
374- alpha = 0.7
375- )
376-
377- # if trendline_fit == "LOWESS":
378- # # Fit LOWESS curve, using statsmodels.nonparametric.smoothers_lowess
379- # lowess_fit = lowess(y_sorted, x_sorted, frac=0.4) # frac for smoothness (0.2 = wiggly, 0.6 = smoother)
380- # x_fit, y_fit = lowess_fit[:, 0], lowess_fit[:, 1]
381- # ax.plot(x_fit, y_fit, linestyle="dotted", color="red", alpha=0.7)
382-
383- # Trendline right
384- x_sorted , y_sorted = get_trendline_values (trend_dict , "R" )
371+ trend_l , = ax .plot (
372+ x_sorted ,
373+ y_sorted ,
374+ linestyle = "dotted" ,
375+ color = "grey" ,
376+ alpha = 0.7
377+ )
385378
386- if trendline_fit == "linear_regression" :
387- coeffs = np .polyfit (x_sorted , y_sorted , 1 )
388- poly_fn = np .poly1d (coeffs )
389- ax .plot (
390- x_sorted ,
391- poly_fn (x_sorted ),
392- linestyle = "dashed" ,
393- color = "blue" ,
394- alpha = 0.7
395- )
396- # if trendline_fit == "LOWESS":
397- # # Fit LOWESS curve, using statsmodels.nonparametric.smoothers_lowess
398- # lowess_fit = lowess(y_sorted, x_sorted, frac=0.4) # frac for smoothness (0.2 = wiggly, 0.6 = smoother)
399- # x_fit, y_fit = lowess_fit[:, 0], lowess_fit[:, 1]
400- # ax.plot(x_fit, y_fit, linestyle="dashed", color="blue", alpha=0.7)
379+ x_sorted , y_sorted = get_trendline_values (trend_dict , "R" )
380+ trend_r , = ax .plot (
381+ x_sorted ,
382+ y_sorted ,
383+ linestyle = "dashed" ,
384+ color = "grey" ,
385+ alpha = 0.7
386+ )
387+ trendline_legend = ax .legend (handles = [trend_l , trend_r ], loc = 'lower center' )
388+ trendline_legend = ax .legend (
389+ handles = [trend_l , trend_r ],
390+ labels = ["Left" , "Right" ],
391+ loc = "lower center" ,
392+ fontsize = legend_size ,
393+ title = "Trendlines"
394+ )
395+ # Add the legend manually to the Axes.
396+ ax .add_artist (trendline_legend )
401397
402398 # Create combined tick positions & labels
403399 main_ticks = range (len (bin_labels ))
@@ -423,6 +419,7 @@ def get_trendline_values(trend_dict, side):
423419 ax .set_title ("Transduction efficiency per octave band (Left/Right)" )
424420
425421 ax .legend (title = "Cochlea" , fontsize = legend_size )
422+
426423 plt .tight_layout ()
427424
428425 if ".png" in save_path :
@@ -471,7 +468,7 @@ def main():
471468
472469 fig_04e (chreef_data ,
473470 save_path = os .path .join (args .figure_dir , f"fig_04e_transduction.{ FILE_EXTENSION } " ),
474- plot = args .plot , use_alias = use_alias , trendlines = False )
471+ plot = args .plot , use_alias = use_alias , trendlines = True )
475472 fig_04e (chreef_data ,
476473 save_path = os .path .join (args .figure_dir , f"fig_04e_intensity.{ FILE_EXTENSION } " ),
477474 plot = args .plot , intensity = True , use_alias = use_alias )
0 commit comments