本文實例講述了Python實現繪制雙柱狀圖并顯示數值功能。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*-#! python3import matplotlib.pyplot as pltimport mpl_toolkits.mplot3d#定義函數來顯示柱狀上的數值def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2.-0.2, 1.03*height, '%s' % float(height))if __name__ == '__main__': l1=[68, 96, 85, 86, 76,87, 95] l2=[85, 68, 79, 89, 94, 82,90] name=['A','B','C','D','E','F','E'] total_width, n = 0.8, 2 width = total_width / n x=[0,1,2,3,4,5,6] plt.rc('font', family='SimHei', size=12)#設置中文顯示,否則出現亂碼! a=plt.bar(x, l1, width=width, label='數學',fc = 'y') for i in range(len(x)): x[i] = x[i] + width b=plt.bar(x, l2, width=width, label='語文',tick_label = name,fc = 'r') autolabel(a) autolabel(b) plt.xlabel('學生') plt.ylabel('成績') plt.title('學生成績') plt.legend() plt.show()
運行結果:
更多關于Python相關內容可查看本站專題:《Python數學運算技巧總結》、《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答