@@ -3816,7 +3816,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
3816
3816
tick_labels = None , flierprops = None , medianprops = None ,
3817
3817
meanprops = None , capprops = None , whiskerprops = None ,
3818
3818
manage_ticks = True , autorange = False , zorder = None ,
3819
- capwidths = None ):
3819
+ capwidths = None , label = None ):
3820
3820
"""
3821
3821
Draw a box and whisker plot.
3822
3822
@@ -4003,6 +4003,20 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
4003
4003
The style of the median.
4004
4004
meanprops : dict, default: None
4005
4005
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
+
4006
4020
data : indexable object, optional
4007
4021
DATA_PARAMETER_PLACEHOLDER
4008
4022
@@ -4123,7 +4137,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
4123
4137
meanline = meanline , showfliers = showfliers ,
4124
4138
capprops = capprops , whiskerprops = whiskerprops ,
4125
4139
manage_ticks = manage_ticks , zorder = zorder ,
4126
- capwidths = capwidths )
4140
+ capwidths = capwidths , label = label )
4127
4141
return artists
4128
4142
4129
4143
def bxp (self , bxpstats , positions = None , widths = None , vert = True ,
@@ -4132,7 +4146,7 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
4132
4146
boxprops = None , whiskerprops = None , flierprops = None ,
4133
4147
medianprops = None , capprops = None , meanprops = None ,
4134
4148
meanline = False , manage_ticks = True , zorder = None ,
4135
- capwidths = None ):
4149
+ capwidths = None , label = None ):
4136
4150
"""
4137
4151
Draw a box and whisker plot from pre-computed statistics.
4138
4152
@@ -4215,6 +4229,20 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
4215
4229
If True, the tick locations and labels will be adjusted to match the
4216
4230
boxplot positions.
4217
4231
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
+
4218
4246
zorder : float, default: ``Line2D.zorder = 2``
4219
4247
The zorder of the resulting boxplot.
4220
4248
@@ -4379,6 +4407,7 @@ def do_patch(xs, ys, **kwargs):
4379
4407
if showbox :
4380
4408
do_box = do_patch if patch_artist else do_plot
4381
4409
boxes .append (do_box (box_x , box_y , ** box_kw ))
4410
+ median_kw .setdefault ('label' , '_nolegend_' )
4382
4411
# draw the whiskers
4383
4412
whisker_kw .setdefault ('label' , '_nolegend_' )
4384
4413
whiskers .append (do_plot (whis_x , whislo_y , ** whisker_kw ))
@@ -4389,7 +4418,6 @@ def do_patch(xs, ys, **kwargs):
4389
4418
caps .append (do_plot (cap_x , cap_lo , ** cap_kw ))
4390
4419
caps .append (do_plot (cap_x , cap_hi , ** cap_kw ))
4391
4420
# draw the medians
4392
- median_kw .setdefault ('label' , '_nolegend_' )
4393
4421
medians .append (do_plot (med_x , med_y , ** median_kw ))
4394
4422
# maybe draw the means
4395
4423
if showmeans :
@@ -4407,6 +4435,18 @@ def do_patch(xs, ys, **kwargs):
4407
4435
flier_y = stats ['fliers' ]
4408
4436
fliers .append (do_plot (flier_x , flier_y , ** flier_kw ))
4409
4437
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
+
4410
4450
if manage_ticks :
4411
4451
axis_name = "x" if vert else "y"
4412
4452
interval = getattr (self .dataLim , f"interval{ axis_name } " )
0 commit comments