@@ -2498,7 +2498,7 @@ def barh(self, y, width, height=0.8, left=None, *, align="center",
24982498 align = align , ** kwargs )
24992499 return patches
25002500
2501- def bar_label (self , container , captions = [] , * , fmt = "%g" , position = "edge" ,
2501+ def bar_label (self , container , labels = None , * , fmt = "%g" , position = "edge" ,
25022502 padding = 0 , shifting = 0 , ** kwargs ):
25032503 """
25042504 Label a bar plot.
@@ -2511,7 +2511,7 @@ def bar_label(self, container, captions=[], *, fmt="%g", position="edge",
25112511 container : `.BarContainer`
25122512 Container with all the bars and optionally errorbars.
25132513
2514- captions : array-like, optional
2514+ labels : array-like, optional
25152515 A list of label texts, that should be displayed. If not given, the
25162516 label texts will be the data values formatted with *fmt*.
25172517
@@ -2521,23 +2521,26 @@ def bar_label(self, container, captions=[], *, fmt="%g", position="edge",
25212521 position : {'edge', 'center'}, default: 'edge'
25222522 Position of the label relative to the bar:
25232523
2524- - 'edge': Placed at edge, the cumulative values will be shown.
2525- - 'center': Placed at center, the individual values will be shown.
2524+ - 'edge': the value is the position of the edge.
2525+ (cumulative for stacked bars)
2526+ - 'center': the value shown will be the length of the bar.
25262527
25272528 padding : float, default: 0
2528- Offset in points for label to shift in longitudinal direction .
2529+ Offset in points parallel to the direction of the bar .
25292530
25302531 shifting : float, default: 0
2531- Offset in points for label to shift in lateral direction.
2532+ Offset in points perpendicular to the direction of the bar.
2533+
2534+ **kwargs is passed through to `.Axes.annotate`.
25322535
25332536 Returns
25342537 -------
25352538 annotations
25362539 A list of `.Text` instances for the labels.
25372540 """
2538- def extend_with_none (iterable , length ):
2539- return list (iterable ) + [None ] * (length - len (iterable ))
25402541
2542+ # want to know whether to put label on positive or negative direction
2543+ # cannot use np.sign here because it will return 0 if x == 0
25412544 def sign (x ):
25422545 return 1 if x >= 0 else - 1
25432546
@@ -2547,19 +2550,17 @@ def sign(x):
25472550 errorbar = container .errorbar
25482551 orientation = container .orientation
25492552
2550- N = len (bars )
25512553 if errorbar :
25522554 lines = errorbar .lines
25532555 barlinecols = lines [2 ]
25542556 barlinecol = barlinecols [0 ]
2555- segments = barlinecol .get_segments ()
2556- errs = extend_with_none (segments , length = N )
2557+ errs = barlinecol .get_segments ()
25572558 else :
2558- errs = extend_with_none ([], length = N )
2559- caps = extend_with_none ( captions , length = N )
2559+ errs = []
2560+ lbls = [] if labels is None else labels
25602561
25612562 annotations = []
2562- for bar , err , cap in zip (bars , errs , caps ):
2563+ for bar , err , lbl in itertools . zip_longest (bars , errs , lbls ):
25632564
25642565 (x0 , y0 ), (x1 , y1 ) = bar .get_bbox ().get_points ()
25652566 xc , yc = (x0 + x1 ) / 2 , (y0 + y1 ) / 2
@@ -2608,8 +2609,8 @@ def sign(x):
26082609 elif position == "edge" and orientation == "horizontal" and xc < 0 :
26092610 ha , va = "right" , "center"
26102611
2611- annotation = self .annotate (cap or fmt % value , xy , xytext ,
2612- textcoords = "offset points" ,
2612+ annotation = self .annotate (fmt % value if lbl is None else lbl ,
2613+ xy , xytext , textcoords = "offset points" ,
26132614 ha = ha , va = va , ** kwargs )
26142615 annotations .append (annotation )
26152616
0 commit comments