Python實現(xiàn)繪制雙柱狀圖并顯示數(shù)值功能示例
更新時間:2018年06月23日 02:13:57 作者:水之魂2018
這篇文章主要介紹了Python實現(xiàn)繪制雙柱狀圖并顯示數(shù)值功能,涉及Python數(shù)值運算及基于matplotlib的圖形繪制相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)繪制雙柱狀圖并顯示數(shù)值功能。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*-
#! python3
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
#定義函數(shù)來顯示柱狀上的數(shù)值
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)#設(shè)置中文顯示,否則出現(xiàn)亂碼!
a=plt.bar(x, l1, width=width, label='數(shù)學(xué)',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('學(xué)生')
plt.ylabel('成績')
plt.title('學(xué)生成績')
plt.legend()
plt.show()
運行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
解決Python中報錯TypeError: must be str, not bytes問題
這篇文章主要介紹了解決Python中報錯TypeError: must be str, not bytes問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

