今天想看下兩組資料的對比情況,就給畫了個圖:
classify = ['accept', 'reject'] # 姓名
education = list(ac['index'])
counts = (list(ac['education']), list(re['education']))
# 設定柱形圖寬度
bar_width = 0.35
index = np.arange(len(counts[0]))
# 繪制
rects1 = plt.bar(index, counts[0], bar_width, color='#0072BC', label=classify[0])
# 繪制
rects2 = plt.bar(index + bar_width, counts[1], bar_width, color='#ED1C24', label=classify[1])
# X軸标題
plt.xticks(index + bar_width, education)
# Y軸範圍
plt.ylim(ymax=3000, ymin=0)
# 圖表标題
plt.title('the education of accept, reject')
# 圖例顯示在圖表下方
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.03), fancybox=True, ncol=5)
# 添加資料标簽
def add_labels(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2, height, height, ha='center', va='bottom')
# 柱形圖邊緣用白色填充,純粹為了美觀
rect.set_edgecolor('white')
add_labels(rects1)
add_labels(rects2)