|
3 | 3 | Grouped bar chart with labels
|
4 | 4 | =============================
|
5 | 5 |
|
6 |
| -Bar charts are useful for visualizing counts, or summary statistics |
7 |
| -with error bars. This example shows a ways to create a grouped bar chart |
8 |
| -with Matplotlib and also how to annotate bars with labels. |
| 6 | +This example shows a how to create a grouped bar chart and how to annotate |
| 7 | +bars with labels. |
9 | 8 | """
|
10 | 9 |
|
11 | 10 | import matplotlib
|
12 | 11 | import matplotlib.pyplot as plt
|
13 | 12 | import numpy as np
|
14 | 13 |
|
15 | 14 |
|
16 |
| -men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2) |
17 |
| -women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3) |
| 15 | +labels = ['G1', 'G2', 'G3', 'G4', 'G5'] |
| 16 | +men_means = [20, 34, 30, 35, 27] |
| 17 | +women_means = [25, 32, 34, 20, 25] |
18 | 18 |
|
19 |
| -ind = np.arange(len(men_means)) # the x locations for the groups |
| 19 | +x = np.arange(len(labels)) # the label locations |
20 | 20 | width = 0.35 # the width of the bars
|
21 | 21 |
|
22 | 22 | fig, ax = plt.subplots()
|
23 |
| -rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std, |
24 |
| - label='Men') |
25 |
| -rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std, |
26 |
| - label='Women') |
| 23 | +rects1 = ax.bar(x - width/2, men_means, width, label='Men') |
| 24 | +rects2 = ax.bar(x + width/2, women_means, width, label='Women') |
27 | 25 |
|
28 | 26 | # Add some text for labels, title and custom x-axis tick labels, etc.
|
29 | 27 | ax.set_ylabel('Scores')
|
30 | 28 | ax.set_title('Scores by group and gender')
|
31 |
| -ax.set_xticks(ind) |
32 |
| -ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) |
| 29 | +ax.set_xticks(x) |
| 30 | +ax.set_xticklabels(labels) |
33 | 31 | ax.legend()
|
34 | 32 |
|
35 | 33 |
|
36 |
| -def autolabel(rects, xpos='center'): |
37 |
| - """ |
38 |
| - Attach a text label above each bar in *rects*, displaying its height. |
39 |
| -
|
40 |
| - *xpos* indicates which side to place the text w.r.t. the center of |
41 |
| - the bar. It can be one of the following {'center', 'right', 'left'}. |
42 |
| - """ |
43 |
| - |
44 |
| - ha = {'center': 'center', 'right': 'left', 'left': 'right'} |
45 |
| - offset = {'center': 0, 'right': 1, 'left': -1} |
46 |
| - |
| 34 | +def autolabel(rects): |
| 35 | + """Attach a text label above each bar in *rects*, displaying its height.""" |
47 | 36 | for rect in rects:
|
48 | 37 | height = rect.get_height()
|
49 | 38 | ax.annotate('{}'.format(height),
|
50 | 39 | xy=(rect.get_x() + rect.get_width() / 2, height),
|
51 |
| - xytext=(offset[xpos]*3, 3), # use 3 points offset |
52 |
| - textcoords="offset points", # in both directions |
53 |
| - ha=ha[xpos], va='bottom') |
| 40 | + xytext=(0, 3), # 3 points vertical offset |
| 41 | + textcoords="offset points", |
| 42 | + ha='center', va='bottom') |
54 | 43 |
|
55 | 44 |
|
56 |
| -autolabel(rects1, "left") |
57 |
| -autolabel(rects2, "right") |
| 45 | +autolabel(rects1) |
| 46 | +autolabel(rects2) |
58 | 47 |
|
59 | 48 | fig.tight_layout()
|
60 | 49 |
|
61 | 50 | plt.show()
|
62 | 51 |
|
63 |
| - |
64 | 52 | #############################################################################
|
65 | 53 | #
|
66 | 54 | # ------------
|
|
0 commit comments