Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitdbe0502

Browse files
authored
Merge pull request#9907 from afvincent/enh_annotated_bar_example
DOC: (subjectively) nicer annotated barchart example
2 parentsc9f80ef +f872c37 commitdbe0502

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

‎examples/api/barchart.py

Lines 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
importnumpyasnp
99
importmatplotlib.pyplotasplt
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-
defautolabel(rects):
31+
defautolabel(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
forrectinrects:
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)

[8]ページ先頭

©2009-2025 Movatter.jp