python3使用matplotlib繪制條形圖
本文實(shí)例為大家分享了python3使用matplotlib繪制條形圖的具體代碼,供大家參考,具體內(nèi)容如下
繪制豎狀條形圖
代碼
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/ukai.ttc")
a = ["戰(zhàn)狼2", "速度與激情8", "功夫瑜伽", "西游伏妖篇", "變形金剛5:最后的騎士", "摔跤吧!爸爸", "加勒比海盜5:死無對證", "金剛:骷髏島", "極限特工:終極回歸", "生化危機(jī)6:終章",
"乘風(fēng)破浪", "神偷奶爸3", "智取威虎山", "大鬧天竺", "金剛狼3:殊死一戰(zhàn)", "蜘蛛俠:英雄歸來", "悟空傳", "銀河護(hù)衛(wèi)隊(duì)2", "情圣", "新木乃伊", ]
b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88,
6.86, 6.58, 6.23]
plt.figure(figsize=(15,7))
# 繪制條形圖
plt.bar(range(len(a)),b,width=0.3)
# 對應(yīng)x軸與字符串
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)
plt.savefig("./bar1.png")
plt.show()
效果圖

繪制橫狀條形圖
代碼
# 繪制橫著的條形圖
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/ukai.ttc")
a = ["戰(zhàn)狼2", "速度與激情8", "功夫瑜伽", "西游伏妖篇", "變形金剛5:最后的騎士", "摔跤吧!爸爸", "加勒比海盜5:死無對證", "金剛:骷髏島", "極限特工:終極回歸", "生化危機(jī)6:終章",
"乘風(fēng)破浪", "神偷奶爸3", "智取威虎山", "大鬧天竺", "金剛狼3:殊死一戰(zhàn)", "蜘蛛俠:英雄歸來", "悟空傳", "銀河護(hù)衛(wèi)隊(duì)2", "情圣", "新木乃伊", ]
b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88,
6.86, 6.58, 6.23]
plt.figure(figsize=(15, 7))
# 繪制條形圖
plt.barh(range(len(a)), b, height=0.3,color='orange')
# 對應(yīng)x軸與字符串
plt.yticks(range(len(a)), a, fontproperties=my_font, rotation=0)
# 添加網(wǎng)格 alpha參數(shù)是設(shè)置網(wǎng)格的透明度的
plt.grid(alpha=0.3)
# 保存圖片
plt.savefig("./bar1.png")
plt.show()
效果圖

需要注意的是橫著的和豎著的條形圖的區(qū)別在與橫著的使用的是barh()方法,同時要注意它傳參數(shù)的順序是:
def barh(y, width, height=0.8, left=None, *, align='center', **kwargs):
繪制多次條形圖
代碼
from matplotlib import pyplot as plt
from matplotlib import font_manager
myfont = font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/ukai.ttc")
a = ["猩球崛起3:終極之戰(zhàn)","敦刻爾克","蜘蛛俠:英雄歸來","戰(zhàn)狼2"]
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]
bar_width = 0.25
x_14 = list(range(len(a)))
x_15 = list(i+bar_width for i in x_14)
x_16 = list(i+bar_width for i in x_15)
# 設(shè)置圖形大小
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(len(a)),b_14,width=bar_width,label="9月14日")
plt.bar(x_15,b_15,width=bar_width,label="9月15日")
plt.bar(x_16,b_16,width=bar_width,label="9月16日")
# 設(shè)置圖例
plt.legend(prop=myfont)
# 設(shè)置x軸刻度
plt.xticks(x_15,a,fontproperties=myfont)
plt.savefig("./mutiy.png")
plt.show()
效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python openpyxl模塊實(shí)現(xiàn)excel讀寫操作
這篇文章主要介紹了Python openpyxl模塊實(shí)現(xiàn)excel讀寫操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Python實(shí)現(xiàn)的讀取電腦硬件信息功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的讀取電腦硬件信息功能,結(jié)合實(shí)例形式分析了Python基于wmi庫讀取電腦CPU、磁盤、網(wǎng)絡(luò)、進(jìn)程等硬件信息相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Python解析json之ValueError: Expecting property name enclosed in
這篇文章主要給大家介紹了關(guān)于Python解析json報(bào)錯:ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)的解決方法,文中介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07
Python根據(jù)區(qū)號生成手機(jī)號碼的方法
這篇文章主要介紹了Python根據(jù)區(qū)號生成手機(jī)號碼的方法,涉及Python隨機(jī)數(shù)與字符串的相關(guān)操作技巧,需要的朋友可以參考下2015-07-07
通過Python實(shí)現(xiàn)自動填寫調(diào)查問卷
這篇文章主要介紹了通過Python實(shí)現(xiàn)自動填寫調(diào)查問卷的相關(guān)資料,需要的朋友可以參考下2017-09-09

