欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python繪制條形圖方法代碼詳解

 更新時(shí)間:2017年12月19日 14:38:07   作者:-dragon-  
這篇文章主要介紹了python繪制條形圖方法代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。

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ù)參考本站:

python繪制鉛球的運(yùn)行軌跡代碼分享

用Pygal繪制直方圖代碼示例

Python中pygal繪制雷達(dá)圖代碼分享

如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

最新評(píng)論