Skip to content

Commit e4608c1

Browse files
authored
Merge pull request #12244 from anntzer/merge-barchart-examples
Merge barchart examples.
2 parents 19701f5 + 5608ac3 commit e4608c1

File tree

3 files changed

+36
-80
lines changed

3 files changed

+36
-80
lines changed

examples/lines_bars_and_markers/barchart.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

examples/statistics/barchart_demo.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
88
Bar charts are useful for visualizing counts, or summary statistics
99
with error bars. These examples show a few ways to do this with Matplotlib.
10-
1110
"""
1211

12+
###############################################################################
13+
# A bar plot with errorbars and height labels on individual bars.
14+
1315
# Credit: Josh Hemann
1416

1517
import numpy as np
@@ -18,41 +20,47 @@
1820
from collections import namedtuple
1921

2022

21-
n_groups = 5
23+
men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
24+
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
2225

23-
means_men = (20, 35, 30, 35, 27)
24-
std_men = (2, 3, 4, 1, 2)
25-
26-
means_women = (25, 32, 34, 20, 25)
27-
std_women = (3, 5, 2, 3, 3)
26+
ind = np.arange(len(men_means)) # the x locations for the groups
27+
width = 0.35 # the width of the bars
2828

2929
fig, ax = plt.subplots()
30+
rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
31+
color='SkyBlue', label='Men')
32+
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
33+
color='IndianRed', label='Women')
3034

31-
index = np.arange(n_groups)
32-
bar_width = 0.35
35+
# Add some text for labels, title and custom x-axis tick labels, etc.
36+
ax.set_ylabel('Scores')
37+
ax.set_title('Scores by group and gender')
38+
ax.set_xticks(ind)
39+
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
40+
ax.legend()
3341

34-
opacity = 0.4
35-
error_config = {'ecolor': '0.3'}
3642

37-
rects1 = ax.bar(index, means_men, bar_width,
38-
alpha=opacity, color='b',
39-
yerr=std_men, error_kw=error_config,
40-
label='Men')
43+
def autolabel(rects, xpos='center'):
44+
"""
45+
Attach a text label above each bar in *rects*, displaying its height.
4146
42-
rects2 = ax.bar(index + bar_width, means_women, bar_width,
43-
alpha=opacity, color='r',
44-
yerr=std_women, error_kw=error_config,
45-
label='Women')
47+
*xpos* indicates which side to place the text w.r.t. the center of
48+
the bar. It can be one of the following {'center', 'right', 'left'}.
49+
"""
50+
51+
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
52+
offset = {'center': 0.5, 'right': 0.57, 'left': 0.43} # x_txt = x + w*off
53+
54+
for rect in rects:
55+
height = rect.get_height()
56+
ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
57+
'{}'.format(height), ha=ha[xpos], va='bottom')
4658

47-
ax.set_xlabel('Group')
48-
ax.set_ylabel('Scores')
49-
ax.set_title('Scores by group and gender')
50-
ax.set_xticks(index + bar_width / 2)
51-
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))
52-
ax.legend()
59+
60+
autolabel(rects1, "left")
61+
autolabel(rects2, "right")
5362

5463
fig.tight_layout()
55-
plt.show()
5664

5765

5866
###############################################################################

examples/units/bar_unit_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Group barchart with units
44
=========================
55
6-
This is the same example as
7-
:doc:`the barchart demo<../lines_bars_and_markers/barchart>` in centimeters.
6+
This is the same example as :doc:`/gallery/statistics/barchart_demo` in
7+
centimeters.
88
99
.. only:: builder_html
1010

0 commit comments

Comments
 (0)