@@ -212,6 +212,10 @@ def statsplot(
212212 ax = None ,
213213):
214214 """Main function for plotting statistical tests."""
215+
216+ default_box_params = {"fliersize" : 0 if show_dots else None }
217+ default_swarm_params = {}
218+
215219 if ax is None :
216220 ax = plt .subplot (111 )
217221
@@ -228,10 +232,15 @@ def statsplot(
228232
229233 if order_test is None :
230234 order_test = unique (test_variable )
235+ else :
236+ assert set (order_test ) == set (
237+ test_variable
238+ ), "test_variable has more values than order test. order_test cannot be used to subset the data. Do this prior."
231239
232240 # use subgrouping
233241 if grouping_variable is None :
234242 params .update (dict (x = test_variable , order = order_test ))
243+ default_swarm_params .update (dict (color = "k" ))
235244 else :
236245
237246 if type (grouping_variable ) == str :
@@ -242,34 +251,37 @@ def statsplot(
242251
243252 if order_grouping is None :
244253 order_grouping = unique (grouping_variable )
254+ else :
245255
246- params .update (
247- dict (
248- x = grouping_variable ,
249- order = order_grouping ,
250- hue = test_variable ,
251- hue_order = order_test ,
252- )
253- )
256+ assert set (order_grouping ) >= set (
257+ grouping_variable
258+ ), "grouping_variable has more values than order_grouping. order_grouping cannot be used to subset the data. Do this prior."
259+
260+ params .update (dict (x = grouping_variable , order = order_grouping ))
254261
262+ default_swarm_params .update (dict (dodge = True , palette = "dark:k" ,hue = test_variable , hue_order = order_test ,legend = False ))
263+
264+ # apply new keyword params
255265 if box_params is None :
256266 box_params = {}
267+ box_params = {** default_box_params , ** box_params }
257268
258269 if swarm_params is None :
259270 swarm_params = {}
260-
261- sns .boxplot (palette = palette , ** params , ** box_params )
271+ swarm_params = {** default_swarm_params , ** swarm_params }
272+
273+ sns .boxplot (
274+ palette = palette ,
275+ legend = grouping_variable is not None ,
276+ hue_order = order_test ,
277+ hue = test_variable ,
278+ ** params ,
279+ ** box_params ,
280+ )
262281
263282 if show_dots :
264- legend = ax .get_legend_handles_labels ()
265-
266- sns .swarmplot (** params , palette = "dark:k" , dodge = True , ** swarm_params )
267-
268- # add old variable, as dots have unified colors
269- if grouping_variable is not None :
270- ax .legend (
271- * legend , bbox_to_anchor = (1 , 1 ), title = ax .legend_ .get_title ().get_text ()
272- )
283+ logger .debug (f"Showing dots with params: { dict (** params , ** swarm_params )} " )
284+ sns .swarmplot (legend = False , ** params , ** swarm_params )
273285
274286 # Statistics
275287 if p_values is None :
0 commit comments