|
7 | 7 |
|
8 | 8 | Bar charts are useful for visualizing counts, or summary statistics |
9 | 9 | with error bars. These examples show a few ways to do this with Matplotlib. |
10 | | -
|
11 | 10 | """ |
12 | 11 |
|
| 12 | +############################################################################### |
| 13 | +# A bar plot with errorbars and height labels on individual bars. |
| 14 | + |
13 | 15 | # Credit: Josh Hemann |
14 | 16 |
|
15 | 17 | import numpy as np |
|
18 | 20 | from collections import namedtuple |
19 | 21 |
|
20 | 22 |
|
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) |
22 | 25 |
|
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 |
28 | 28 |
|
29 | 29 | 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') |
30 | 34 |
|
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() |
33 | 41 |
|
34 | | -opacity = 0.4 |
35 | | -error_config = {'ecolor': '0.3'} |
36 | 42 |
|
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. |
41 | 46 |
|
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') |
46 | 58 |
|
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") |
53 | 62 |
|
54 | 63 | fig.tight_layout() |
55 | | -plt.show() |
56 | 64 |
|
57 | 65 |
|
58 | 66 | ############################################################################### |
|
0 commit comments