Skip to content

Commit fdd426c

Browse files
committed
remove arg 'shifting', rename arg 'label_type', improve docstring
1 parent 19642c5 commit fdd426c

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

examples/lines_bars_and_markers/bar_label_demo.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@
6666
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
6767
ax.legend()
6868

69-
# Label with position 'center' instead of the default 'edge'
70-
ax.bar_label(p1, position='center')
71-
ax.bar_label(p2, position='center')
69+
# Label with label_type 'center' instead of the default 'edge'
70+
ax.bar_label(p1, label_type='center')
71+
ax.bar_label(p2, label_type='center')
7272
ax.bar_label(p2)
7373

7474
plt.show()
@@ -112,12 +112,10 @@
112112
ax.set_xlabel('Performance')
113113
ax.set_title('How fast do you want to go today?')
114114

115-
# Label with given captions, custom padding, shifting and annotate option
116-
arrowprops = dict(color='b', arrowstyle="-|>",
117-
connectionstyle="angle,angleA=0,angleB=90,rad=20")
115+
# Label with given captions, custom padding and annotate options
118116
ax.bar_label(hbars, labels=['±%.2f' % e for e in error],
119-
padding=30, shifting=20, arrowprops=arrowprops, color='b')
120-
ax.set_xlim(right=17)
117+
padding=8, color='b', fontsize=14)
118+
ax.set_xlim(right=16)
121119

122120
plt.show()
123121

lib/matplotlib/axes/_axes.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,8 +2498,8 @@ 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, labels=None, *, fmt="%g", position="edge",
2502-
padding=0, shifting=0, **kwargs):
2501+
def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
2502+
padding=0, **kwargs):
25032503
"""
25042504
Label a bar plot.
25052505
@@ -2518,18 +2518,18 @@ def bar_label(self, container, labels=None, *, fmt="%g", position="edge",
25182518
fmt : str, default: '%g'
25192519
A format string for the label.
25202520
2521-
position : {'edge', 'center'}, default: 'edge'
2522-
Position of the label relative to the bar:
2521+
label_type : {'edge', 'center'}, default: 'edge'
2522+
The label type, Possible values:
25232523
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.
2524+
- 'edge': label placed at the end-point of the bar segment, and the
2525+
value displayed will be the position of that end-point.
2526+
- 'center': label placed in the center of the bar segment, and the
2527+
value displayed will be the length of that segment.
2528+
(useful for stacked bars, i.e.
2529+
:doc:`/gallery/lines_bars_and_markers/bar_label_demo`)
25272530
25282531
padding : float, default: 0
2529-
Offset in points parallel to the direction of the bar.
2530-
2531-
shifting : float, default: 0
2532-
Offset in points perpendicular to the direction of the bar.
2532+
Distance of label from the end of the bar.
25332533
25342534
**kwargs : passed through to `.Axes.annotate`.
25352535
@@ -2544,7 +2544,7 @@ def bar_label(self, container, labels=None, *, fmt="%g", position="edge",
25442544
def sign(x):
25452545
return 1 if x >= 0 else -1
25462546

2547-
cbook._check_in_list(['edge', 'center'], position=position)
2547+
cbook._check_in_list(['edge', 'center'], label_type=label_type)
25482548

25492549
bars = container.patches
25502550
errorbar = container.errorbar
@@ -2579,35 +2579,34 @@ def sign(x):
25792579
elif orientation == "horizontal":
25802580
endpt = err[:, 0].max() if xc >= 0 else err[:, 0].min()
25812581

2582-
if position == "center":
2582+
if label_type == "center":
25832583
value = sign(extrema) * length
2584-
elif position == "edge":
2584+
elif label_type == "edge":
25852585
value = extrema
25862586

2587-
if position == "center":
2587+
if label_type == "center":
25882588
xy = xc, yc
2589-
elif position == "edge" and orientation == "vertical":
2589+
elif label_type == "edge" and orientation == "vertical":
25902590
xy = xc, endpt
2591-
elif position == "edge" and orientation == "horizontal":
2591+
elif label_type == "edge" and orientation == "horizontal":
25922592
xy = endpt, yc
25932593

25942594
if orientation == "vertical":
2595-
xytext = shifting, sign(extrema) * padding
2595+
xytext = 0, sign(extrema) * padding
25962596
else:
2597-
xytext = sign(extrema) * padding, shifting
2597+
xytext = sign(extrema) * padding, 0
25982598

2599-
if position == "center":
2599+
if label_type == "center":
26002600
ha, va = "center", "center"
2601-
elif position == "edge" and orientation == "vertical" and yc >= 0:
2602-
ha, va = "center", "bottom"
2603-
elif position == "edge" and orientation == "vertical" and yc < 0:
2604-
ha, va = "center", "top"
2605-
elif (
2606-
position == "edge" and orientation == "horizontal" and xc >= 0
2607-
):
2608-
ha, va = "left", "center"
2609-
elif position == "edge" and orientation == "horizontal" and xc < 0:
2610-
ha, va = "right", "center"
2601+
elif label_type == "edge":
2602+
if orientation == "vertical" and yc >= 0:
2603+
ha, va = "center", "bottom"
2604+
elif orientation == "vertical" and yc < 0:
2605+
ha, va = "center", "top"
2606+
elif orientation == "horizontal" and xc >= 0:
2607+
ha, va = "left", "center"
2608+
elif orientation == "horizontal" and xc < 0:
2609+
ha, va = "right", "center"
26112610

26122611
annotation = self.annotate(fmt % value if lbl is None else lbl,
26132612
xy, xytext, textcoords="offset points",

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6814,7 +6814,7 @@ def test_bar_label_location_center():
68146814
ax = plt.gca()
68156815
ys, widths = [1, 2], [3, -4]
68166816
rects = ax.barh(ys, widths)
6817-
labels = ax.bar_label(rects, position='center')
6817+
labels = ax.bar_label(rects, label_type='center')
68186818
assert labels[0].xy == (widths[0] / 2, ys[0])
68196819
assert labels[0].get_ha() == 'center'
68206820
assert labels[0].get_va() == 'center'

0 commit comments

Comments
 (0)