# matplotlib的官方简单条形图的例子
import numpy as np
import matplotlib.pyplot as plt
men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
ind = np.arange(len(men_means)) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
color='SkyBlue', label='Men')
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
color='IndianRed', label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.legend()
def autolabel(rects, xpos='center'):
"""
Attach a text label above each bar in *rects*, displaying its height.
*xpos* indicates which side to place the text w.r.t. the center of
the bar. It can be one of the following {'center', 'right', 'left'}.
"""
xpos = xpos.lower() # normalize the case of the parameter
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
offset = {'center': 0.5, 'right': 0.57, 'left': 0.43} # x_txt = x + w*off
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
'{}'.format(height), ha=ha[xpos], va='bottom')
autolabel(rects1, "left")
autolabel(rects2, "right")
plt.show()
Figure_1.png
#我画的
import numpy as np
import matplotlib.pyplot as plt
men_means = (20, 35, 30, 35, 27)
women_means = (40, 40, 40, 40, 40)
x_ticks = np.arange(5)
x_ticklabels = (('G1', 'G2', 'G3', 'G4','G5'))
width = 0.35
fig, ax = plt.subplots()
pic1 = ax.bar(x_ticks-width/2, men_means, width=width, label="Men")
pic2 = ax.bar(x_ticks+width/2, women_means, width=width, label="Women" )
ax.set_title('Score by group and gender')
ax.set_ylabel('Score')
ax.set_xlabel('Group')
ax.set_ylim((15,60))
ax.set_xticks(x_ticks)
ax.set_xticklabels(x_ticklabels)
for a, b, c in zip(x_ticks, men_means, women_means):
ax.text(a-width/2, b+1, b, ha='center', va='bottom')
ax.text(a+width/2, c+1, c, ha='center', va='bottom')
Figure_1.png
import numpy as np
import matplotlib.pyplot as plt
men = (183, 188, 177, 180, 185)
women = (165, 160, 167, 158, 162)
ind = np.arange(5)
m_label = ('m1', 'm2', 'm3', 'm4', 'm5')
w_label = ('w1', 'w2', 'w3', 'w4', 'w5')
g_label = ('g1', 'g2', 'g3', 'g4', 'g5')
fig, axes = plt.subplots(2,2)
# 这些值都是从0开始的
axes[0,0].bar(ind, men, color='blue')
axes[0,0].set_xticks(ind)
axes[0,0].set_xticklabels(m_label)
axes[0,0].set_ylim(0,200)
axes[0,1].bar(ind, women, color='red')
axes[0,1].set_xticks(ind)
axes[0,1].set_xticklabels(w_label)
axes[0,1].set_ylim(0,200)
wid = 0.35
axes[1,0].bar(ind-wid/2, men, width = wid, label="Men")
axes[1,0].bar(ind+wid/2, women, width = wid, label="Women")
axes[1,0].set_xticks(ind)
axes[1,0].set_xticklabels(g_label)
axes[1,0].set_xlabel("Gender")
axes[1,0].set_ylabel("Height")
axes[1,0].set_ylim(150,200)
axes[1,0].set_yticks((160,170,180,190,200))
axes[1,0].legend()
axes[1,1].barh(ind-wid/2, men, height = wid, label="Men")
axes[1,1].barh(ind+wid/2, women, height = wid, label="Women")
axes[1,1].set_yticks(ind)
axes[1,1].set_yticklabels(g_label)
axes[1,1].set_xlim(100,200)
for a, b, c in zip(ind, men, women):
axes[1,1].text(b+2, a-wid/2, b)
axes[1,1].text(c+2, a+wid/2, c)
Figure_1.png
网友评论