@@ -3765,15 +3765,16 @@ def apply_mask(arrays, mask):
37653765 return errorbar_container # (l0, caplines, barcols)
37663766
37673767 @_preprocess_data ()
3768+ @_api .rename_parameter ("3.9" , "labels" , "tick_labels" )
37683769 def boxplot (self , x , notch = None , sym = None , vert = None , whis = None ,
37693770 positions = None , widths = None , patch_artist = None ,
37703771 bootstrap = None , usermedians = None , conf_intervals = None ,
37713772 meanline = None , showmeans = None , showcaps = None ,
37723773 showbox = None , showfliers = None , boxprops = None ,
3773- labels = None , flierprops = None , medianprops = None ,
3774+ tick_labels = None , flierprops = None , medianprops = None ,
37743775 meanprops = None , capprops = None , whiskerprops = None ,
37753776 manage_ticks = True , autorange = False , zorder = None ,
3776- capwidths = None ):
3777+ capwidths = None , label = None ):
37773778 """
37783779 Draw a box and whisker plot.
37793780
@@ -3884,10 +3885,12 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
38843885 If `False` produces boxes with the Line2D artist. Otherwise,
38853886 boxes are drawn with Patch artists.
38863887
3887- labels : sequence, optional
3888+ tick_labels : sequence, optional
38883889 Labels for each dataset (one per dataset). These are used for
38893890 x-tick labels; *not* for legend entries.
38903891
3892+ .. versionadded:: 3.9
3893+
38913894 manage_ticks : bool, default: True
38923895 If True, the tick locations and labels will be adjusted to match
38933896 the boxplot positions.
@@ -3956,6 +3959,10 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
39563959 The style of the mean.
39573960 data : indexable object, optional
39583961 DATA_PARAMETER_PLACEHOLDER
3962+ label : sequence, optional
3963+ Legend labels for each boxplot.
3964+
3965+ .. versionadded:: 3.9
39593966
39603967 See Also
39613968 --------
@@ -3970,7 +3977,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
39703977 bootstrap = mpl .rcParams ['boxplot.bootstrap' ]
39713978
39723979 bxpstats = cbook .boxplot_stats (x , whis = whis , bootstrap = bootstrap ,
3973- labels = labels , autorange = autorange )
3980+ tick_labels = tick_labels , autorange = autorange )
39743981 if notch is None :
39753982 notch = mpl .rcParams ['boxplot.notch' ]
39763983 if vert is None :
@@ -4006,6 +4013,9 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
40064013 if 'color' in boxprops :
40074014 boxprops ['edgecolor' ] = boxprops .pop ('color' )
40084015
4016+ if label :
4017+ boxprops ['label' ] = label
4018+
40094019 # if non-default sym value, put it into the flier dictionary
40104020 # the logic for providing the default symbol ('b+') now lives
40114021 # in bxp in the initial value of flierkw
@@ -4330,8 +4340,8 @@ def do_patch(xs, ys, **kwargs):
43304340 if showbox :
43314341 do_box = do_patch if patch_artist else do_plot
43324342 boxes .append (do_box (box_x , box_y , ** box_kw ))
4343+ whisker_kw .setdefault ('label' , '_nolegend_' )
43334344 # draw the whiskers
4334- whisker_kw .setdefault ('label' , '_nolegend_' )
43354345 whiskers .append (do_plot (whis_x , whislo_y , ** whisker_kw ))
43364346 whiskers .append (do_plot (whis_x , whishi_y , ** whisker_kw ))
43374347 # maybe draw the caps
@@ -4358,6 +4368,22 @@ def do_patch(xs, ys, **kwargs):
43584368 flier_y = stats ['fliers' ]
43594369 fliers .append (do_plot (flier_x , flier_y , ** flier_kw ))
43604370
4371+ # Set legend labels
4372+ if 'label' in boxprops :
4373+ # If showbox=False, label will be passed to whiskers instead.
4374+ box_or_whis = boxes if showbox else whiskers
4375+ for index , element in enumerate (box_or_whis ):
4376+ try :
4377+ if isinstance (boxprops ['label' ], list ):
4378+ element .set_label (boxprops ['label' ][index ])
4379+ else :
4380+ element .set_label (boxprops ['label' ])
4381+ # If only one label is passed, stop it from being repeated
4382+ # on each legend handle.
4383+ boxprops ['label' ] = ''
4384+ except Exception :
4385+ IndexError ('List index out of range' )
4386+
43614387 if manage_ticks :
43624388 axis_name = "x" if vert else "y"
43634389 interval = getattr (self .dataLim , f"interval{ axis_name } " )
0 commit comments