Matplotlib柱狀圖加百分比解決辦法

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
score = pd.read_excel('E:/Data/前測自我認同感量表.xls',sheet_name = 'Sheet2')
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.figure(figsize=(20,10),dpi=100)
plt.bar(score['自我認同總分'],score['人數'],width=0.5,label = '人數',color='orange')
plt.xlabel('自我認同總分',fontsize=14,fontweight='bold')
plt.ylabel('人數',fontsize=14,fontweight='bold')
plt.title('中職學生自我認同總分分佈表',fontsize=16,fontweight='bold')
plt.legend()
count=0
Sum=score['人數'].sum()
for i in score['人數']:
    plt.text(count,i+0.5, str('{:.2f}'.format(score['人數'][count]/Sum *100)) +'%', \
    ha='center') #位置,高度,內容,居中
    plt.text(count,i+2.5, str('{}'.format(i)+'人'), ha='center')
    count+=1 #增加百分比
plt.savefig('E:/Data/2.png')
plt.show()

結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章