@@ -3798,7 +3798,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
3798
3798
tick_labels = None , flierprops = None , medianprops = None ,
3799
3799
meanprops = None , capprops = None , whiskerprops = None ,
3800
3800
manage_ticks = True , autorange = False , zorder = None ,
3801
- capwidths = None ):
3801
+ capwidths = None , label = None ):
3802
3802
"""
3803
3803
Draw a box and whisker plot.
3804
3804
@@ -3985,6 +3985,18 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
3985
3985
The style of the median.
3986
3986
meanprops : dict, default: None
3987
3987
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
+
3988
4000
data : indexable object, optional
3989
4001
DATA_PARAMETER_PLACEHOLDER
3990
4002
@@ -4105,7 +4117,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
4105
4117
meanline = meanline , showfliers = showfliers ,
4106
4118
capprops = capprops , whiskerprops = whiskerprops ,
4107
4119
manage_ticks = manage_ticks , zorder = zorder ,
4108
- capwidths = capwidths )
4120
+ capwidths = capwidths , label = label )
4109
4121
return artists
4110
4122
4111
4123
def bxp (self , bxpstats , positions = None , widths = None , vert = True ,
@@ -4114,7 +4126,7 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
4114
4126
boxprops = None , whiskerprops = None , flierprops = None ,
4115
4127
medianprops = None , capprops = None , meanprops = None ,
4116
4128
meanline = False , manage_ticks = True , zorder = None ,
4117
- capwidths = None ):
4129
+ capwidths = None , label = None ):
4118
4130
"""
4119
4131
Draw a box and whisker plot from pre-computed statistics.
4120
4132
@@ -4197,6 +4209,18 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
4197
4209
If True, the tick locations and labels will be adjusted to match the
4198
4210
boxplot positions.
4199
4211
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
+
4200
4224
zorder : float, default: ``Line2D.zorder = 2``
4201
4225
The zorder of the resulting boxplot.
4202
4226
@@ -4361,6 +4385,7 @@ def do_patch(xs, ys, **kwargs):
4361
4385
if showbox :
4362
4386
do_box = do_patch if patch_artist else do_plot
4363
4387
boxes .append (do_box (box_x , box_y , ** box_kw ))
4388
+ median_kw .setdefault ('label' , '_nolegend_' )
4364
4389
# draw the whiskers
4365
4390
whisker_kw .setdefault ('label' , '_nolegend_' )
4366
4391
whiskers .append (do_plot (whis_x , whislo_y , ** whisker_kw ))
@@ -4371,7 +4396,6 @@ def do_patch(xs, ys, **kwargs):
4371
4396
caps .append (do_plot (cap_x , cap_lo , ** cap_kw ))
4372
4397
caps .append (do_plot (cap_x , cap_hi , ** cap_kw ))
4373
4398
# draw the medians
4374
- median_kw .setdefault ('label' , '_nolegend_' )
4375
4399
medians .append (do_plot (med_x , med_y , ** median_kw ))
4376
4400
# maybe draw the means
4377
4401
if showmeans :
@@ -4389,6 +4413,19 @@ def do_patch(xs, ys, **kwargs):
4389
4413
flier_y = stats ['fliers' ]
4390
4414
fliers .append (do_plot (flier_x , flier_y , ** flier_kw ))
4391
4415
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
+
4392
4429
if manage_ticks :
4393
4430
axis_name = "x" if vert else "y"
4394
4431
interval = getattr (self .dataLim , f"interval{ axis_name } " )
0 commit comments