|
7 | 7 | using `~matplotlib.pyplot.bar`. Note the parameters *yerr* used for
|
8 | 8 | error bars, and *bottom* to stack the women's bars on top of the men's
|
9 | 9 | bars.
|
10 |
| -
|
11 | 10 | """
|
12 | 11 |
|
13 | 12 | import numpy as np
|
14 | 13 | import matplotlib.pyplot as plt
|
15 | 14 |
|
16 | 15 |
|
17 | 16 | N = 5
|
18 |
| -menMeans = (20, 35, 30, 35, 27) |
19 |
| -womenMeans = (25, 32, 34, 20, 25) |
20 |
| -menStd = (2, 3, 4, 1, 2) |
21 |
| -womenStd = (3, 5, 2, 3, 3) |
| 17 | +men_means = [20, 35, 30, 35, 27] |
| 18 | +women_means = [25, 32, 34, 20, 25] |
| 19 | +men_std = [2, 3, 4, 1, 2] |
| 20 | +women_std = [3, 5, 2, 3, 3] |
22 | 21 | ind = np.arange(N) # the x locations for the groups
|
23 | 22 | width = 0.35 # the width of the bars: can also be len(x) sequence
|
24 | 23 |
|
25 |
| -p1 = plt.bar(ind, menMeans, width, yerr=menStd) |
26 |
| -p2 = plt.bar(ind, womenMeans, width, |
27 |
| - bottom=menMeans, yerr=womenStd) |
| 24 | +plt.bar(ind, men_means, width, yerr=men_std, label='Men') |
| 25 | +plt.bar(ind, women_means, width, yerr=women_std, bottom=men_means, |
| 26 | + label='Women') |
28 | 27 |
|
29 | 28 | plt.ylabel('Scores')
|
30 | 29 | plt.title('Scores by group and gender')
|
31 | 30 | plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
|
32 | 31 | plt.yticks(np.arange(0, 81, 10))
|
33 |
| -plt.legend((p1[0], p2[0]), ('Men', 'Women')) |
| 32 | +plt.legend() |
34 | 33 |
|
35 | 34 | plt.show()
|
0 commit comments