Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f872c37

Browse filesBrowse files
committed
clean and enhance annotated barchart example
1 parent c9f80ef commit f872c37
Copy full SHA for f872c37

File tree

Expand file treeCollapse file tree

1 file changed

+27
-22
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+27
-22
lines changed

‎examples/api/barchart.py

Copy file name to clipboardExpand all lines: examples/api/barchart.py
+27-22Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,50 @@
33
Barchart
44
========
55
6-
A bar plot with errorbars and height labels on individual bars
6+
A bar plot with errorbars and height labels on individual bars.
77
"""
88
import numpy as np
99
import matplotlib.pyplot as plt
1010

11-
N = 5
12-
men_means = (20, 35, 30, 35, 27)
13-
men_std = (2, 3, 4, 1, 2)
11+
men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
12+
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
1413

15-
ind = np.arange(N) # the x locations for the groups
16-
width = 0.35 # the width of the bars
14+
ind = np.arange(len(men_means)) # the x locations for the groups
15+
width = 0.35 # the width of the bars
1716

1817
fig, ax = plt.subplots()
19-
rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std)
18+
rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
19+
color='SkyBlue', label='Men')
20+
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
21+
color='IndianRed', label='Women')
2022

21-
women_means = (25, 32, 34, 20, 25)
22-
women_std = (3, 5, 2, 3, 3)
23-
rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std)
24-
25-
# add some text for labels, title and axes ticks
23+
# Add some text for labels, title and custom x-axis tick labels, etc.
2624
ax.set_ylabel('Scores')
2725
ax.set_title('Scores by group and gender')
28-
ax.set_xticks(ind + width / 2)
26+
ax.set_xticks(ind)
2927
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
30-
31-
ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))
28+
ax.legend()
3229

3330

34-
def autolabel(rects):
31+
def autolabel(rects, xpos='center'):
3532
"""
36-
Attach a text label above each bar displaying its height
33+
Attach a text label above each bar in *rects*, displaying its height.
34+
35+
*xpos* indicates which side to place the text w.r.t. the center of
36+
the bar. It can be one of the following {'center', 'right', 'left'}.
3737
"""
38+
39+
xpos = xpos.lower() # normalize the case of the parameter
40+
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
41+
offset = {'center': 0.5, 'right': 0.57, 'left': 0.43} # x_txt = x + w*off
42+
3843
for rect in rects:
3944
height = rect.get_height()
40-
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
41-
'%d' % int(height),
42-
ha='center', va='bottom')
45+
ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
46+
'{}'.format(height), ha=ha[xpos], va='bottom')
47+
4348

44-
autolabel(rects1)
45-
autolabel(rects2)
49+
autolabel(rects1, "left")
50+
autolabel(rects2, "right")
4651

4752
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.