python繪制條形圖方法代碼詳解
1.首先要繪制一個(gè)簡(jiǎn)單的條形圖
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') plt.show()
1.1上面中rects=plt.bar(left=(0.2,1),height=(1,0.5),width=0.2,align=”center”,yerr=0.000001)這句代碼是最重要的,其中l(wèi)eft表示直方圖的開始的位置(也就是最左邊的地方),height是指直方圖的高度,當(dāng)直方圖太粗時(shí),可以通過(guò)width來(lái)定義直方圖的寬度,注意多個(gè)直方圖要用元組,yerr這個(gè)參數(shù)是防止直方圖觸頂。
2.增加直方圖腳注
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') plt.xticks((0.2,1),('frst','second')) plt.show()
3.條形圖上顯示具體的數(shù)字(自動(dòng)編號(hào))
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height)) autolabel(rects) plt.xticks((0.2,1),('frst','second')) plt.show()
4.改變顏色
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),color=('r','g'),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height)) autolabel(rects) plt.xticks((0.2,1),('frst','second')) plt.show()
5.添加圖注
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects1 =plt.bar(left = (0.2),height = (0.5),color=('g'),label=(('no1')),width = 0.2,align="center",yerr=0.000001) rects2 =plt.bar(left = (1),height = (1),color=('r'),label=(('no2')),width = 0.2,align="center",yerr=0.000001) plt.legend() plt.xticks((0.2,1),('frst','second')) plt.title('Pe') def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height)) autolabel(rects1) autolabel(rects2) plt.show()
6大家根據(jù)自己的需要自己來(lái)繪制自己的條形圖
下面回答網(wǎng)友提問(wèn),如何畫在條形圖上垂直顯示數(shù)據(jù):
下面這個(gè)函數(shù)是用來(lái)垂直顯示的,其中設(shè)置角度就可以以任意方式來(lái)顯示。
def autolabel(rects,Num=1.12,rotation1=90,NN=1): for rect in rects: height = rect.get_height() plt.text(rect.get_x()-0.04+rect.get_width()/2., Num*height, '%s' % float(height*NN),rotation=rotation1)
調(diào)用方式如下
rects1 =plt.bar(left = (0.05),height = (Pe_FH),color=('b'),label=('FHMM'),width = 0.1,align="center",yerr=0.000001); autolabel(rects1,1.09);
下面是效果圖
總結(jié)
以上就是本文關(guān)于python繪制條形圖方法代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參考本站:
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python first-order-model實(shí)現(xiàn)讓照片動(dòng)起來(lái)
本文將利用first-order-model實(shí)現(xiàn)讓照片動(dòng)起來(lái),除了表情驅(qū)動(dòng)照片,還可以姿態(tài)遷移。文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下2022-06-06用Python刪除本地目錄下某一時(shí)間點(diǎn)之前創(chuàng)建的所有文件的實(shí)例
下面小編就為大家分享一篇用Python刪除本地目錄下某一時(shí)間點(diǎn)之前創(chuàng)建的所有文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12pytorch 實(shí)現(xiàn)張量tensor,圖片,CPU,GPU,數(shù)組等的轉(zhuǎn)換
今天小編就為大家分享一篇pytorch 實(shí)現(xiàn)張量tensor,圖片,CPU,GPU,數(shù)組等的轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01python連接并簡(jiǎn)單操作SQL?server數(shù)據(jù)庫(kù)詳細(xì)步驟
python作為一門十分火熱的編程語(yǔ)言,操作數(shù)據(jù)庫(kù)自然是必不可少的,下面這篇文章主要給大家介紹了關(guān)于python連接并簡(jiǎn)單操作SQL?server數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2023-06-06手把手教你使用Python解決簡(jiǎn)單的zip文件解壓密碼
本文主要介紹了Python解決簡(jiǎn)單的zip文件解壓密碼,使用的核心模塊是python標(biāo)準(zhǔn)庫(kù)中的zipfile模塊。具有一定的參考價(jià)值,感興趣的可以了解一下2021-11-11Python基礎(chǔ)知識(shí)學(xué)習(xí)之類的繼承
今天帶大家學(xué)習(xí)Python的基礎(chǔ)知識(shí),文中對(duì)python類的繼承作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05Python?中的對(duì)象析構(gòu)函數(shù)__del__?詳情
這篇文章主要介紹了Python?中的對(duì)象析構(gòu)函數(shù)del詳情,Python?中的類的構(gòu)造函數(shù)???__init__???,?每當(dāng)實(shí)例產(chǎn)生就會(huì)調(diào)用這個(gè)構(gòu)造函下面更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-03-03