Skip to content

Commit de9a2d8

Browse files
committed
named bar_label, add arg-validation, tests and whats_new
1 parent d82409a commit de9a2d8

File tree

5 files changed

+79
-16
lines changed

5 files changed

+79
-16
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:orphan:
2+
3+
Bar charts auto-labeling
4+
------------------------
5+
A new `.Axes.bar_label` method has been added for auto-labeling bar charts.
6+
See :doc:`/gallery/lines_bars_and_markers/bar_label_demo` for examples.
7+

examples/lines_bars_and_markers/bar_label_demo.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
Bar Label Demo
44
==============
55
6-
This example shows how to use `blabel` helper function
6+
This example shows how to use `bar_label` helper function
77
to create bar chart labels.
88
9-
See also the :doc:`grouped bar chart
9+
See also the :doc:`grouped bar
1010
</gallery/lines_bars_and_markers/barchart>`,
11-
:doc:`stacked bar graph
11+
:doc:`stacked bar
1212
</gallery/lines_bars_and_markers/bar_stacked>` and
1313
:doc:`horizontal bar chart
1414
</gallery/lines_bars_and_markers/barh>` examples.
@@ -44,8 +44,8 @@
4444
ax1.legend()
4545

4646
# Basic labels
47-
ax1.blabel(rects1)
48-
ax1.blabel(rects2)
47+
ax1.bar_label(rects1)
48+
ax1.bar_label(rects2)
4949

5050
plt.show()
5151

@@ -65,9 +65,9 @@
6565
ax2.legend()
6666

6767
# Label with 'center' mode instead of the default 'edge' mode
68-
ax2.blabel(p1, mode='center')
69-
ax2.blabel(p2, mode='center')
70-
ax2.blabel(p2)
68+
ax2.bar_label(p1, mode='center')
69+
ax2.bar_label(p2, mode='center')
70+
ax2.bar_label(p2)
7171

7272
plt.show()
7373

@@ -93,7 +93,7 @@
9393
ax3.set_title('How fast do you want to go today?')
9494

9595
# Label with specially formatted floats
96-
ax3.blabel(hbars1, fmt='%.2f')
96+
ax3.bar_label(hbars1, fmt='%.2f')
9797

9898
plt.show()
9999

@@ -113,8 +113,8 @@
113113
arrowprops = dict(color='b', arrowstyle="-|>",
114114
connectionstyle="angle,angleA=0,angleB=90,rad=20")
115115

116-
ax4.blabel(hbars2, captions=['±%.2f' % e for e in error],
117-
padding=30, shifting=20, arrowprops=arrowprops, color='b')
116+
ax4.bar_label(hbars2, captions=['±%.2f' % e for e in error],
117+
padding=30, shifting=20, arrowprops=arrowprops, color='b')
118118

119119
plt.show()
120120

@@ -132,5 +132,5 @@
132132
matplotlib.pyplot.bar
133133
matplotlib.axes.Axes.barh
134134
matplotlib.pyplot.barh
135-
matplotlib.axes.Axes.blabel
136-
matplotlib.pyplot.blabel
135+
matplotlib.axes.Axes.bar_label
136+
matplotlib.pyplot.bar_label

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 2 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 blabel(self, container, captions=[], fmt="%g", mode="edge",
2502-
padding=None, shifting=0, autoscale=True, **kwargs):
2501+
def bar_label(self, container, captions=[], fmt="%g", mode="edge",
2502+
padding=None, shifting=0, autoscale=True, **kwargs):
25032503
"""
25042504
Label a bar plot.
25052505
@@ -2543,6 +2543,7 @@ def nonefill(a, n):
25432543
def sign(x):
25442544
return 1 if x >= 0 else -1
25452545

2546+
cbook._check_in_list(['edge', 'center'], mode=mode)
25462547
if mode == "edge":
25472548
padding = padding or 3
25482549
elif mode == "center":

lib/matplotlib/tests/test_axes.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6782,3 +6782,58 @@ def test_ylabel_ha_with_position(ha):
67826782
ax.set_ylabel("test", y=1, ha=ha)
67836783
ax.yaxis.set_label_position("right")
67846784
assert ax.yaxis.get_label().get_ha() == ha
6785+
6786+
6787+
def test_bar_label_location_vertical():
6788+
ax = plt.gca()
6789+
xs, heights = [1, 2], [3, -4]
6790+
rects = ax.bar(xs, heights)
6791+
labels = ax.bar_label(rects)
6792+
assert labels[0].xy == (xs[0], heights[0])
6793+
assert labels[0].get_ha() == 'center'
6794+
assert labels[0].get_va() == 'bottom'
6795+
assert labels[1].xy == (xs[1], heights[1])
6796+
assert labels[1].get_ha() == 'center'
6797+
assert labels[1].get_va() == 'top'
6798+
6799+
6800+
def test_bar_label_location_horizontal():
6801+
ax = plt.gca()
6802+
ys, widths = [1, 2], [3, -4]
6803+
rects = ax.barh(ys, widths)
6804+
labels = ax.bar_label(rects)
6805+
assert labels[0].xy == (widths[0], ys[0])
6806+
assert labels[0].get_ha() == 'left'
6807+
assert labels[0].get_va() == 'center'
6808+
assert labels[1].xy == (widths[1], ys[1])
6809+
assert labels[1].get_ha() == 'right'
6810+
assert labels[1].get_va() == 'center'
6811+
6812+
6813+
def test_bar_label_location_center():
6814+
ax = plt.gca()
6815+
ys, widths = [1, 2], [3, -4]
6816+
rects = ax.barh(ys, widths)
6817+
labels = ax.bar_label(rects, mode='center')
6818+
assert labels[0].xy == (widths[0] / 2, ys[0])
6819+
assert labels[0].get_ha() == 'center'
6820+
assert labels[0].get_va() == 'center'
6821+
assert labels[1].xy == (widths[1] / 2, ys[1])
6822+
assert labels[1].get_ha() == 'center'
6823+
assert labels[1].get_va() == 'center'
6824+
6825+
6826+
def test_bar_label_fmt():
6827+
ax = plt.gca()
6828+
rects = ax.bar([1, 2], [3, -4])
6829+
labels = ax.bar_label(rects, fmt='%.2f')
6830+
assert labels[0].get_text() == '3.00'
6831+
assert labels[1].get_text() == '-4.00'
6832+
6833+
6834+
def test_bar_label_captions():
6835+
ax = plt.gca()
6836+
rects = ax.bar([1, 2], [3, -4])
6837+
labels = ax.bar_label(rects, captions=['A', 'B'])
6838+
assert labels[0].get_text() == 'A'
6839+
assert labels[1].get_text() == 'B'

tools/boilerplate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def boilerplate_gen():
199199
'bar',
200200
'barbs',
201201
'barh',
202-
'blabel',
202+
'bar_label',
203203
'boxplot',
204204
'broken_barh',
205205
'cla',

0 commit comments

Comments
 (0)