99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀python可視化:matplotlib繪制直方圖進(jìn)階篇
python可視化:matplotlib繪制直方圖進(jìn)階篇
2020-06-03
收藏

在上篇文章中介紹了matplotlib繪制直方圖的前五個(gè)參數(shù),實(shí)際上直方圖一共有十幾個(gè)參數(shù),剩下的參數(shù)利用這篇文章解釋清楚,讓大家能夠?qū)⑷绾卫L制直方圖理解的透透的。

bottom參數(shù)

這個(gè)參數(shù)的含義也很直觀,底部的意思,指的是條形的底從哪里開(kāi)始。這個(gè)參數(shù)接收標(biāo)量和序列,或者None,默認(rèn)為None,如果是標(biāo)量,則所有條形的底都從同一個(gè)數(shù)值處開(kāi)始,如果為序列,則可以指定每個(gè)條形的底不一致。

fig = plt.figure(figsize=(16,4))

pic1 = fig.add_subplot(131)
plt.hist(data,bins = 10)
plt.title("bottom默認(rèn)None")

pic2 = fig.add_subplot(132)
plt.hist(data,bins = 10, bottom=10) # bottom=10,表示所有條形的底部從10開(kāi)始,默認(rèn)從0開(kāi)始
plt.title("bottom=10")

pic3 = fig.add_subplot(133)
plt.hist(data,bins = 10, bottom=np.array([21, 20, 13, 17, 22, 32, 23, 15, 22, 15])) # bottom為序列,序列長(zhǎng)度于條形的數(shù)量一致,表示每個(gè)條的底部從哪里開(kāi)始
plt.title("bottom取值為序列");

上圖是當(dāng)bottom參數(shù)不同取值時(shí)繪制出來(lái)不同的直方圖,第一幅圖和第二幅圖看起來(lái)長(zhǎng)的一樣,但是仔細(xì)觀察下就能發(fā)現(xiàn)兩幅圖y軸的起始點(diǎn)時(shí)不一樣的,第一幅圖的起點(diǎn)時(shí)0.第二幅圖的起點(diǎn)是10,因?yàn)閎ottom參數(shù)設(shè)置的為10;而第三幅圖bottom的參數(shù)設(shè)置的序列,序列的長(zhǎng)度和直方圖的組數(shù)一致,即每個(gè)條形的起始點(diǎn)都不同,具體設(shè)置哪種比較好,還是要看具體的業(yè)務(wù)需求哦。

histtype參數(shù)

histtype參數(shù)控制的時(shí)直方圖中條形的展現(xiàn)方式,它接收的參數(shù)是固定的字符串,其中常見(jiàn)的是以下兩種形式:

fig = plt.figure(figsize=(9,4))

pic1 = fig.add_subplot(121)
plt.hist(data,bins = 10, histtype = "bar")
plt.title(' histtype = "bar"')

pic2 = fig.add_subplot(122)
plt.hist(data,bins = 10, histtype = "step") #  histtype默認(rèn)參數(shù)為”bar“,即條形,可以進(jìn)行指定
plt.title(' histtype = "step"');

如果需要將折線圖直方圖繪制到一副圖中,可以考慮對(duì)條形的形式進(jìn)行設(shè)置。

align參數(shù)

align參數(shù)控制的是條形的位置,能夠接收的參數(shù)也是指定的字符串,通常大家都用默認(rèn)值"mid",即中間,這樣直方圖中的條形會(huì)居于前后臨界點(diǎn)的中間位置,是最常見(jiàn)的一種:

fig = plt.figure(figsize=(16,4))

pic1 = fig.add_subplot(131)
plt.hist(data,bins = 10, align = "left")
plt.xticks([150. , 152.9, 155.8, 158.7, 161.6, 164.5, 167.4, 170.3, 173.2,176.1, 179. ],rotation = 30)
plt.title("align ='left'")

pic2 = fig.add_subplot(132)
plt.hist(data,bins = 10, align = "right") 
plt.xticks([150. , 152.9, 155.8, 158.7, 161.6, 164.5, 167.4, 170.3, 173.2,176.1, 179. ],rotation = 30)
plt.title("align ='right'")

pic3 = fig.add_subplot(133)
plt.hist(data,bins = 10, align = "mid")
plt.xticks([150. , 152.9, 155.8, 158.7, 161.6, 164.5, 167.4, 170.3, 173.2,176.1, 179. ],rotation = 30)
plt.title("align ='mid'"); 

由于原數(shù)據(jù)和分箱規(guī)則沒(méi)有變化,所以三個(gè)直方圖很相近,區(qū)別在于x軸上,這里為了能直觀的看出區(qū)別,特意將每組的臨界值添加到了x軸,仔細(xì)查看能夠看出只有最后一個(gè)圖才是我們常見(jiàn)的直方圖,前兩個(gè)條形的位置都有偏移。

orientation參數(shù)

對(duì)條形圖比較熟悉的朋友可能對(duì)這個(gè)參數(shù)并不陌生,它是控制條形方向的參數(shù),接收的是特定的字符,即條形的方向是垂直的還是水平的,一般默認(rèn)繪制的都是垂直方向的,如果需要橫向的直方圖,直接設(shè)置這個(gè)參數(shù)就好。它接收的參數(shù)是指定的字符串,表明條形方向:

fig = plt.figure(figsize=(9,4))

pic1 = fig.add_subplot(121)
plt.hist(data,bins = 10) #默認(rèn)條形方向?yàn)榇怪狈较?
plt.title('orientation默認(rèn)"vertical"')

pic2 = fig.add_subplot(122)
plt.hist(data,bins = 10,orientation = 'horizontal') #  orientation = 'horizontal'表示條形為水平方向
plt.title('orientation = "horizontal"');

參數(shù)中可選的兩個(gè)字符即是垂直還是水平。

rwidth參數(shù)

從字面上看,這個(gè)參數(shù)是和寬度有關(guān)的,事實(shí)也的確是這樣。這個(gè)參數(shù)可以設(shè)置條形的寬度,接收數(shù)值,但是它設(shè)置的寬度是相對(duì)于默認(rèn)寬度而言的,重新設(shè)置的寬度是原寬度的幾分之幾,我們具體看一下代碼:

fig = plt.figure(figsize=(9,4))

pic1 = fig.add_subplot(121)
plt.hist(data,bins = 10) #rwidth控制條形的相對(duì)寬度,不進(jìn)行指定,自動(dòng)計(jì)算
plt.title('rwidth默認(rèn)"None"')

pic2 = fig.add_subplot(122)
plt.hist(data,bins = 10,rwidth=0.8) # 指定條形的相對(duì)寬度
plt.title('rwidth=0.8');

如果不進(jìn)行設(shè)置,直方圖的各個(gè)條形之間是沒(méi)有空隙的,當(dāng)我將rwidth設(shè)置成0.8之后,條形的寬度就只有原寬度的80%,條形之間也會(huì)出現(xiàn)縫隙。

log參數(shù)

log參數(shù)控制是否將刻度設(shè)置成對(duì)數(shù)刻度,接收布爾值,默認(rèn)為False,進(jìn)行普通刻度,一旦設(shè)置為True:

fig = plt.figure(figsize=(9,4))

pic1 = fig.add_subplot(121)
plt.hist(data,bins = 10)
plt.title('log默認(rèn)"False"')

pic2 = fig.add_subplot(122)
plt.hist(data,bins = 10,log=True) # 直方圖軸將設(shè)置為對(duì)數(shù)刻度。
plt.title('log=True"');

設(shè)置成對(duì)數(shù)刻度后,雖然分組情況沒(méi)有變,但是分布狀況還是發(fā)生了變化,如果設(shè)置了該參數(shù),最好在標(biāo)題或其他部分標(biāo)注提示一下。

color參數(shù)和X

這個(gè)參數(shù)可以說(shuō)是相當(dāng)熟悉,很多函數(shù)中都有,表示對(duì)圖形的顏色進(jìn)行設(shè)置,沒(méi)錯(cuò)的確是設(shè)置顏色,想起我們還有一個(gè)最開(kāi)始的參數(shù)沒(méi)有講解,就在這里和color一起講解了。

目前我們只有一組數(shù)據(jù),現(xiàn)在呢假設(shè)有了兩個(gè)學(xué)校學(xué)生的身高數(shù)據(jù),喏,這就是第二個(gè)學(xué)校的學(xué)生身高了:

data1 = np.random.randint(150,180,200)
data1

輸出結(jié)果:

array([164, 171, 172, 161, 171, 175, 161, 170, 159, 163, 154, 162, 156,
       158, 160, 156, 163, 167, 170, 168, 163, 171, 174, 161, 156, 167,
       165, 169, 162, 176, 167, 157, 157, 169, 160, 177, 162, 154, 163,
       168, 155, 177, 151, 155, 179, 166, 170, 168, 158, 167, 156, 170,
       163, 157, 172, 169, 156, 171, 155, 160, 177, 164, 157, 160, 173,
       175, 164, 168, 171, 158, 163, 162, 167, 167, 169, 155, 175, 171,
       162, 174, 165, 179, 167, 179, 168, 157, 151, 151, 171, 170, 168,
       165, 167, 179, 153, 177, 165, 155, 153, 157, 162, 167, 173, 161,
       171, 159, 165, 152, 160, 172, 154, 157, 176, 152, 171, 161, 169,
       154, 171, 150, 158, 164, 150, 170, 153, 162, 150, 174, 150, 176,
       167, 171, 164, 170, 171, 163, 162, 164, 174, 157, 179, 166, 150,
       170, 166, 161, 155, 175, 163, 156, 152, 159, 168, 158, 176, 159,
       158, 169, 155, 166, 151, 163, 177, 154, 170, 152, 167, 172, 170,
       163, 161, 177, 164, 160, 157, 167, 163, 177, 169, 162, 166, 158,
       156, 168, 169, 168, 159, 159, 154, 169, 168, 169, 156, 165, 173,
       175, 169, 156, 158, 154])

到這里是不是有點(diǎn)明白了,直方圖不止可以對(duì)一組數(shù)據(jù)進(jìn)行繪圖,多組數(shù)據(jù)也是可以的,參數(shù)x可以接收多組數(shù)據(jù),如果是多組數(shù)據(jù)需要將多組數(shù)據(jù)打包到一起作為一個(gè)整體傳給參數(shù)x:

fig = plt.figure(figsize=(16,4))

pic1 = fig.add_subplot(131)
plt.hist(data,bins = 10)
plt.title("color默認(rèn)None")

pic2 = fig.add_subplot(132)
plt.hist(data,bins = 10, color="r") # 設(shè)置顏色為紅色
plt.title("color="r"")

pic3 = fig.add_subplot(133)
plt.hist([data,data1],bins = 10, color=["c","orange"]) 
# color取值為序列,每個(gè)數(shù)據(jù)集對(duì)應(yīng)一種顏色,color序列的長(zhǎng)度與數(shù)據(jù)集個(gè)數(shù)一致
plt.title("color取值為序列");

能夠看到,如果只有一組數(shù)據(jù),color參數(shù)也就只接收一個(gè)顏色指定,如果是多組數(shù)據(jù),可以對(duì)每個(gè)數(shù)據(jù)集的顏色進(jìn)行指定,具體需要注意的點(diǎn)已經(jīng)在代碼中備注了哦。

label參數(shù)

label參數(shù)也不是陌生的參數(shù),是對(duì)標(biāo)簽的設(shè)定,接收的是字符串,并沒(méi)有什么特殊,但是需要注意的是如果設(shè)置了這個(gè)參數(shù),記得調(diào)用plt.lenged()顯示圖例,如果不調(diào)用即使設(shè)置了標(biāo)簽也不能作為圖例顯示在圖形中。而顯示圖例除了直接在直方圖函數(shù)中設(shè)置label參數(shù)外,還可以在plt.lenged()中設(shè)置,具體的區(qū)別還是看代碼吧:

fig = plt.figure(figsize=(11,4))

pic1 = fig.add_subplot(121)
plt.hist([data,data1],bins = 10, color=["c","orange"],label=["data","data1"])  
plt.legend()
plt.title('hist函數(shù)中設(shè)置label參數(shù)')

pic2 = fig.add_subplot(122)
plt.hist([data,data1],bins = 10, color=["c","orange"])  
plt.legend(["data","data1"])
plt.title('legend函數(shù)中設(shè)置label參數(shù)');

效果是一樣的。

stacked參數(shù)

這個(gè)參數(shù)的字面意思也很直觀,表示是否要堆疊,接收布爾值。需要注意的是如果繪圖只用了一個(gè)數(shù)據(jù)集,那么這個(gè)參數(shù)無(wú)論設(shè)置成什么都沒(méi)有影響,如果要堆疊至少需要兩個(gè)數(shù)據(jù)集才能顯示出區(qū)別:

fig = plt.figure(figsize=(9,4))

pic1 = fig.add_subplot(121)
plt.hist([data,data1],stacked=False)  #默認(rèn)多組數(shù)據(jù)并列排列
plt.title('stacked默認(rèn)"False"')

pic2 = fig.add_subplot(122)
plt.hist([data,data1],stacked=True)  #多組數(shù)據(jù)彼此堆疊
plt.title('stacked=True"');

區(qū)別是不是很明顯了,堆疊的意思也容易理解了對(duì)不對(duì)?

以上就是直方圖函數(shù)所有參數(shù)的設(shè)置講解,希望能夠幫助大家能夠全面的掌握如何繪制一個(gè)符合實(shí)際需求的直方圖。

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }