@@ -3798,7 +3798,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
37983798 tick_labels = None , flierprops = None , medianprops = None ,
37993799 meanprops = None , capprops = None , whiskerprops = None ,
38003800 manage_ticks = True , autorange = False , zorder = None ,
3801- capwidths = None ):
3801+ capwidths = None , label = None ):
38023802 """
38033803 Draw a box and whisker plot.
38043804
@@ -3985,6 +3985,18 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
39853985 The style of the median.
39863986 meanprops : dict, default: None
39873987 The style of the mean.
3988+ label : str or list of str, optional
3989+ Legend labels. Use a single string when all boxes have the same style and
3990+ you only want a single legend entry for them. Use a list of strings to
3991+ label all boxes individually. To be distinguishable, the boxes should be
3992+ styled individually, which is currently only possible by modifying the
3993+ returned artists, see e.g. :doc:`/gallery/statistics/boxplot_demo`.
3994+
3995+ In the case of a single string, the legend entry will technically be
3996+ associated with the first box only. By default, the legend will show the
3997+ median line (``result["medians"]``); if *patch_artist* is True, the legend
3998+ will show the box `Patch` artists (``result["boxes"]``) instead.
3999+
39884000 data : indexable object, optional
39894001 DATA_PARAMETER_PLACEHOLDER
39904002
@@ -4105,7 +4117,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
41054117 meanline = meanline , showfliers = showfliers ,
41064118 capprops = capprops , whiskerprops = whiskerprops ,
41074119 manage_ticks = manage_ticks , zorder = zorder ,
4108- capwidths = capwidths )
4120+ capwidths = capwidths , label = label )
41094121 return artists
41104122
41114123 def bxp (self , bxpstats , positions = None , widths = None , vert = True ,
@@ -4114,7 +4126,7 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
41144126 boxprops = None , whiskerprops = None , flierprops = None ,
41154127 medianprops = None , capprops = None , meanprops = None ,
41164128 meanline = False , manage_ticks = True , zorder = None ,
4117- capwidths = None ):
4129+ capwidths = None , label = None ):
41184130 """
41194131 Draw a box and whisker plot from pre-computed statistics.
41204132
@@ -4197,6 +4209,18 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
41974209 If True, the tick locations and labels will be adjusted to match the
41984210 boxplot positions.
41994211
4212+ label : str or list of str, optional
4213+ Legend labels. Use a single string when all boxes have the same style and
4214+ you only want a single legend entry for them. Use a list of strings to
4215+ label all boxes individually. To be distinguishable, the boxes should be
4216+ styled individually, which is currently only possible by modifying the
4217+ returned artists, see e.g. :doc:`/gallery/statistics/boxplot_demo`.
4218+
4219+ In the case of a single string, the legend entry will technically be
4220+ associated with the first box only. By default, the legend will show the
4221+ median line (``result["medians"]``); if *patch_artist* is True, the legend
4222+ will show the box `Patch` artists (``result["boxes"]``) instead.
4223+
42004224 zorder : float, default: ``Line2D.zorder = 2``
42014225 The zorder of the resulting boxplot.
42024226
@@ -4361,6 +4385,7 @@ def do_patch(xs, ys, **kwargs):
43614385 if showbox :
43624386 do_box = do_patch if patch_artist else do_plot
43634387 boxes .append (do_box (box_x , box_y , ** box_kw ))
4388+ median_kw .setdefault ('label' , '_nolegend_' )
43644389 # draw the whiskers
43654390 whisker_kw .setdefault ('label' , '_nolegend_' )
43664391 whiskers .append (do_plot (whis_x , whislo_y , ** whisker_kw ))
@@ -4371,7 +4396,6 @@ def do_patch(xs, ys, **kwargs):
43714396 caps .append (do_plot (cap_x , cap_lo , ** cap_kw ))
43724397 caps .append (do_plot (cap_x , cap_hi , ** cap_kw ))
43734398 # draw the medians
4374- median_kw .setdefault ('label' , '_nolegend_' )
43754399 medians .append (do_plot (med_x , med_y , ** median_kw ))
43764400 # maybe draw the means
43774401 if showmeans :
@@ -4389,6 +4413,19 @@ def do_patch(xs, ys, **kwargs):
43894413 flier_y = stats ['fliers' ]
43904414 fliers .append (do_plot (flier_x , flier_y , ** flier_kw ))
43914415
4416+ # Set legend labels
4417+ if label :
4418+ box_or_med = boxes if showbox and patch_artist else medians
4419+ if cbook .is_scalar_or_string (label ):
4420+ # assign the label only to the first box
4421+ box_or_med [0 ].set_label (label )
4422+ else : # label is a sequence
4423+ if len (box_or_med ) != len (label ):
4424+ raise ValueError ("There must be an equal number of legend"
4425+ " labels and boxplots." )
4426+ for artist , lbl in zip (box_or_med , label ):
4427+ artist .set_label (lbl )
4428+
43924429 if manage_ticks :
43934430 axis_name = "x" if vert else "y"
43944431 interval = getattr (self .dataLim , f"interval{ axis_name } " )
0 commit comments