@@ -3816,7 +3816,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
38163816 tick_labels = None , flierprops = None , medianprops = None ,
38173817 meanprops = None , capprops = None , whiskerprops = None ,
38183818 manage_ticks = True , autorange = False , zorder = None ,
3819- capwidths = None ):
3819+ capwidths = None , label = None ):
38203820 """
38213821 Draw a box and whisker plot.
38223822
@@ -4003,6 +4003,20 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
40034003 The style of the median.
40044004 meanprops : dict, default: None
40054005 The style of the mean.
4006+ label : str or list of str, optional
4007+ Legend labels. Use a single string when all boxes have the same style and
4008+ you only want a single legend entry for them. Use a list of strings to
4009+ label all boxes individually. To be distinguishable, the boxes should be
4010+ styled individually, which is currently only possible by modifying the
4011+ returned artists, see e.g. :doc:`/gallery/statistics/boxplot_demo`.
4012+
4013+ In the case of a single string, the legend entry will technically be
4014+ associated with the first box only. By default, the legend will show the
4015+ median line (``result["medians"]``); if *patch_artist* is True, the legend
4016+ will show the box `.Patch` artists (``result["boxes"]``) instead.
4017+
4018+ .. versionadded:: 3.9
4019+
40064020 data : indexable object, optional
40074021 DATA_PARAMETER_PLACEHOLDER
40084022
@@ -4123,7 +4137,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
41234137 meanline = meanline , showfliers = showfliers ,
41244138 capprops = capprops , whiskerprops = whiskerprops ,
41254139 manage_ticks = manage_ticks , zorder = zorder ,
4126- capwidths = capwidths )
4140+ capwidths = capwidths , label = label )
41274141 return artists
41284142
41294143 def bxp (self , bxpstats , positions = None , widths = None , vert = True ,
@@ -4132,7 +4146,7 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
41324146 boxprops = None , whiskerprops = None , flierprops = None ,
41334147 medianprops = None , capprops = None , meanprops = None ,
41344148 meanline = False , manage_ticks = True , zorder = None ,
4135- capwidths = None ):
4149+ capwidths = None , label = None ):
41364150 """
41374151 Draw a box and whisker plot from pre-computed statistics.
41384152
@@ -4215,6 +4229,20 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
42154229 If True, the tick locations and labels will be adjusted to match the
42164230 boxplot positions.
42174231
4232+ label : str or list of str, optional
4233+ Legend labels. Use a single string when all boxes have the same style and
4234+ you only want a single legend entry for them. Use a list of strings to
4235+ label all boxes individually. To be distinguishable, the boxes should be
4236+ styled individually, which is currently only possible by modifying the
4237+ returned artists, see e.g. :doc:`/gallery/statistics/boxplot_demo`.
4238+
4239+ In the case of a single string, the legend entry will technically be
4240+ associated with the first box only. By default, the legend will show the
4241+ median line (``result["medians"]``); if *patch_artist* is True, the legend
4242+ will show the box `.Patch` artists (``result["boxes"]``) instead.
4243+
4244+ .. versionadded:: 3.9
4245+
42184246 zorder : float, default: ``Line2D.zorder = 2``
42194247 The zorder of the resulting boxplot.
42204248
@@ -4379,6 +4407,7 @@ def do_patch(xs, ys, **kwargs):
43794407 if showbox :
43804408 do_box = do_patch if patch_artist else do_plot
43814409 boxes .append (do_box (box_x , box_y , ** box_kw ))
4410+ median_kw .setdefault ('label' , '_nolegend_' )
43824411 # draw the whiskers
43834412 whisker_kw .setdefault ('label' , '_nolegend_' )
43844413 whiskers .append (do_plot (whis_x , whislo_y , ** whisker_kw ))
@@ -4389,7 +4418,6 @@ def do_patch(xs, ys, **kwargs):
43894418 caps .append (do_plot (cap_x , cap_lo , ** cap_kw ))
43904419 caps .append (do_plot (cap_x , cap_hi , ** cap_kw ))
43914420 # draw the medians
4392- median_kw .setdefault ('label' , '_nolegend_' )
43934421 medians .append (do_plot (med_x , med_y , ** median_kw ))
43944422 # maybe draw the means
43954423 if showmeans :
@@ -4407,6 +4435,18 @@ def do_patch(xs, ys, **kwargs):
44074435 flier_y = stats ['fliers' ]
44084436 fliers .append (do_plot (flier_x , flier_y , ** flier_kw ))
44094437
4438+ # Set legend labels
4439+ if label :
4440+ box_or_med = boxes if showbox and patch_artist else medians
4441+ if cbook .is_scalar_or_string (label ):
4442+ # assign the label only to the first box
4443+ box_or_med [0 ].set_label (label )
4444+ else : # label is a sequence
4445+ if len (box_or_med ) != len (label ):
4446+ raise ValueError (datashape_message .format ("label" ))
4447+ for artist , lbl in zip (box_or_med , label ):
4448+ artist .set_label (lbl )
4449+
44104450 if manage_ticks :
44114451 axis_name = "x" if vert else "y"
44124452 interval = getattr (self .dataLim , f"interval{ axis_name } " )
0 commit comments