Skip to content

Commit 43c84f5

Browse files
committed
resolve suggested improvements
1 parent b2b8624 commit 43c84f5

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:orphan:
2+
3+
Setting BarContainer orientation
4+
--------------------------------
5+
`.BarContainer` now accepts a new string argument ``orientation``.
6+
It can be either ``vertical`` or ``horizontal``, default is ``None``.
7+

examples/lines_bars_and_markers/bar_label_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
# Label with given captions, custom padding, shifting and annotate option
116116
arrowprops = dict(color='b', arrowstyle="-|>",
117117
connectionstyle="angle,angleA=0,angleB=90,rad=20")
118-
ax.bar_label(hbars, captions=['±%.2f' % e for e in error],
118+
ax.bar_label(hbars, labels=['±%.2f' % e for e in error],
119119
padding=30, shifting=20, arrowprops=arrowprops, color='b')
120120
ax.set_xlim(right=17)
121121

lib/matplotlib/axes/_axes.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6834,6 +6834,6 @@ def test_bar_label_fmt():
68346834
def test_bar_label_captions():
68356835
ax = plt.gca()
68366836
rects = ax.bar([1, 2], [3, -4])
6837-
labels = ax.bar_label(rects, captions=['A', 'B'])
6837+
labels = ax.bar_label(rects, labels=['A', 'B'])
68386838
assert labels[0].get_text() == 'A'
68396839
assert labels[1].get_text() == 'B'

0 commit comments

Comments
 (0)