Skip to content

Commit 63fc476

Browse files
committed
update existing examples, add test for errbars
1 parent 21da4c1 commit 63fc476

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

examples/lines_bars_and_markers/bar_label_demo.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,6 @@
2929
ind = np.arange(N) # the x locations for the groups
3030
width = 0.35 # the width of the bars: can also be len(x) sequence
3131

32-
###############################################################################
33-
# Grouped bar chart
34-
35-
fig, ax = plt.subplots()
36-
37-
rects1 = ax.bar(ind - width/2, menMeans, width, label='Men')
38-
rects2 = ax.bar(ind + width/2, womenMeans, width, label='Women')
39-
40-
ax.axhline(0, color='grey', linewidth=0.8)
41-
ax.set_ylabel('Scores')
42-
ax.set_title('Scores by group and gender')
43-
ax.set_xticks(ind)
44-
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
45-
ax.legend()
46-
47-
# Basic labels
48-
ax.bar_label(rects1)
49-
ax.bar_label(rects2)
50-
51-
plt.show()
52-
5332
###############################################################################
5433
# Stacked bar plot with error bars
5534

examples/lines_bars_and_markers/barchart.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,8 @@
3030
ax.set_xticklabels(labels)
3131
ax.legend()
3232

33-
34-
def autolabel(rects):
35-
"""Attach a text label above each bar in *rects*, displaying its height."""
36-
for rect in rects:
37-
height = rect.get_height()
38-
ax.annotate('{}'.format(height),
39-
xy=(rect.get_x() + rect.get_width() / 2, height),
40-
xytext=(0, 3), # 3 points vertical offset
41-
textcoords="offset points",
42-
ha='center', va='bottom')
43-
44-
45-
autolabel(rects1)
46-
autolabel(rects2)
33+
ax.bar_label(rects1, padding=3)
34+
ax.bar_label(rects2, padding=3)
4735

4836
fig.tight_layout()
4937

@@ -61,5 +49,5 @@ def autolabel(rects):
6149

6250
matplotlib.axes.Axes.bar
6351
matplotlib.pyplot.bar
64-
matplotlib.axes.Axes.annotate
65-
matplotlib.pyplot.annotate
52+
matplotlib.axes.Axes.bar_label
53+
matplotlib.pyplot.bar_label

examples/lines_bars_and_markers/horizontal_barchart_distribution.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ def survey(results, category_names):
5454
for i, (colname, color) in enumerate(zip(category_names, category_colors)):
5555
widths = data[:, i]
5656
starts = data_cum[:, i] - widths
57-
ax.barh(labels, widths, left=starts, height=0.5,
58-
label=colname, color=color)
59-
xcenters = starts + widths / 2
57+
rects = ax.barh(labels, widths, left=starts, height=0.5,
58+
label=colname, color=color)
6059

6160
r, g, b, _ = color
6261
text_color = 'white' if r * g * b < 0.5 else 'darkgrey'
63-
for y, (x, c) in enumerate(zip(xcenters, widths)):
64-
ax.text(x, y, str(int(c)), ha='center', va='center',
65-
color=text_color)
62+
ax.bar_label(rects, label_type='center', color=text_color)
6663
ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1),
6764
loc='lower left', fontsize='small')
6865

@@ -85,7 +82,7 @@ def survey(results, category_names):
8582
import matplotlib
8683
matplotlib.axes.Axes.barh
8784
matplotlib.pyplot.barh
88-
matplotlib.axes.Axes.text
89-
matplotlib.pyplot.text
85+
matplotlib.axes.Axes.bar_label
86+
matplotlib.pyplot.bar_label
9087
matplotlib.axes.Axes.legend
9188
matplotlib.pyplot.legend

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6823,6 +6823,19 @@ def test_bar_label_location_center():
68236823
assert labels[1].get_va() == 'center'
68246824

68256825

6826+
def test_bar_label_location_errorbars():
6827+
ax = plt.gca()
6828+
xs, heights = [1, 2], [3, -4]
6829+
rects = ax.bar(xs, heights, yerr=1)
6830+
labels = ax.bar_label(rects)
6831+
assert labels[0].xy == (xs[0], heights[0] + 1)
6832+
assert labels[0].get_ha() == 'center'
6833+
assert labels[0].get_va() == 'bottom'
6834+
assert labels[1].xy == (xs[1], heights[1] - 1)
6835+
assert labels[1].get_ha() == 'center'
6836+
assert labels[1].get_va() == 'top'
6837+
6838+
68266839
def test_bar_label_fmt():
68276840
ax = plt.gca()
68286841
rects = ax.bar([1, 2], [3, -4])
@@ -6831,7 +6844,7 @@ def test_bar_label_fmt():
68316844
assert labels[1].get_text() == '-4.00'
68326845

68336846

6834-
def test_bar_label_captions():
6847+
def test_bar_label_labels():
68356848
ax = plt.gca()
68366849
rects = ax.bar([1, 2], [3, -4])
68376850
labels = ax.bar_label(rects, labels=['A', 'B'])

0 commit comments

Comments
 (0)