国产午夜男女在线|欧美日本一道高清国产|亚洲日韩乱码中文字幕|麻豆国产97在线精品一区|日韩一区2区三区另类图片|亚洲精品国产99在线观看|亚洲国产午夜福利精品大秀在线|一级做a爰片性色毛片免费网站

          1. <form id="n2a4a"><nav id="n2a4a"></nav></form>
          2. 您當(dāng)前的位置 :寧夏資訊網(wǎng) > 消費(fèi) >  內(nèi)容正文
            投稿

            柱狀圖、堆疊柱狀圖、瀑布圖……有什么區(qū)別?怎樣用Python繪制?

            寧夏資訊網(wǎng) 2020-03-23 08:46:51 來源: 閱讀:-

            導(dǎo)讀:柱狀圖是當(dāng)前應(yīng)用最廣泛的圖表之一,你幾乎每天都可以在電子產(chǎn)品上看到它。它有哪些分類?可以展示哪些數(shù)據(jù)關(guān)系?怎樣用Python繪制?本文帶你逐一了解。

            作者:屈希峰,資深Python工程師,知乎多個專欄作者

            來源:華章科技

            01 概述

            柱狀圖(Histogram)是一種以長方形的長度為變量的表達(dá)圖形的統(tǒng)計(jì)報告圖,由一系列高度不等的縱向條紋表示數(shù)據(jù)分布的情況,用來比較兩個或兩個以上的價值(不同時間或者不同條件),只有一個變量,通常用于較小的數(shù)據(jù)集分析。

            柱狀圖也可橫向排列,或用多維方式表達(dá)。其主要用于數(shù)據(jù)統(tǒng)計(jì)與分析,早期主要用于數(shù)學(xué)統(tǒng)計(jì)學(xué)科中,用柱狀圖表示數(shù)碼相機(jī)的曝光值,到現(xiàn)代使用已經(jīng)比較廣泛,比如現(xiàn)代的電子產(chǎn)品和一些軟件的分析測試,如電腦、數(shù)碼相機(jī)的顯示器和Photoshop上都能看到相應(yīng)的柱狀圖。

            1. 基礎(chǔ)柱狀圖

            基礎(chǔ)柱狀圖經(jīng)常用來對比數(shù)值的大小,使用范圍非常廣泛,例如科比在不同賽季的得分、不同游戲App下載量、不同時期手機(jī)端綜合搜索用戶規(guī)模等,圖2-33顯示不同種類水果的銷量。

            ▲圖2-33 基本柱狀圖

            需要注意的是,分類太多不適合使用豎向柱狀圖,如圖2-34所示。

            ▲圖2-34 豎向柱狀圖

            此時,需要用到橫向柱狀圖,如圖2-35所示。

            ▲圖2-35 橫向柱狀圖

            2. 分組柱狀圖

            分組柱狀圖,又叫聚合柱狀圖。當(dāng)使用者需要在同一個軸上顯示各個分類下不同的分組時,需要用到分組柱狀圖。

            跟柱狀圖類似,使用柱子的高度來映射和對比數(shù)據(jù)值。每個分組中的柱子使用不同顏色或者相同顏色不同透明的方式區(qū)別各個分類,各個分組之間需要保持間隔。

            分組柱狀圖經(jīng)常用于不同組間數(shù)據(jù)的比較,這些組都包含了相同分類的數(shù)據(jù)。例如,展示改革開放以來城鎮(zhèn)與農(nóng)村人口的變化,不同游戲公司的休閑、益智、格斗類App的下載量對比等。圖2-36對比了2015—2017年間不同水果的銷量。

            ▲圖2-36 分組柱狀圖

            3. 堆疊柱狀圖

            與并排顯示分類的分組柱狀圖不同,堆疊柱狀圖將每個柱子進(jìn)行分割以顯示相同類型下各個數(shù)據(jù)的大小情況。它可以形象地展示一個大分類包含的每個小分類的數(shù)據(jù),以及各個小分類的占比,顯示的是單個項(xiàng)目與整體之間的關(guān)系。我們將堆疊柱狀圖分為兩種類型:

            1. 一般的堆疊柱狀圖:每一根柱子上的值分別代表不同的數(shù)據(jù)大小,各層的數(shù)據(jù)總和代表整根柱子的高度。非常適用于比較每個分組的數(shù)據(jù)總量。
            2. 百分比的堆疊柱狀圖:柱子的各個層代表的是該類別數(shù)據(jù)占該分組總體數(shù)據(jù)的百分比。

            堆疊柱狀圖的一個缺點(diǎn)是當(dāng)柱子上的堆疊太多時會導(dǎo)致數(shù)據(jù)很難區(qū)分對比,同時很難對比不同分類下相同維度的數(shù)據(jù),因?yàn)樗鼈儾皇前凑胀换鶞?zhǔn)線對齊的。

            圖2-37是顯示2015—2017年間不同水果的累計(jì)數(shù)量。

            ▲圖2-37 堆疊柱狀圖

            4. 雙向柱狀圖

            雙向柱狀圖,又名正負(fù)條形圖,使用正向和反向的柱子顯示類別之間的數(shù)值比較。其中分類軸表示需要對比的分類維度,連續(xù)軸代表相應(yīng)的數(shù)值,分為兩種情況,一種是正向刻度值與反向刻度值完全對稱,另一種是正向刻度值與反向刻度值反向?qū)ΨQ,即互為相反數(shù)。

            圖2-38是顯示2015—2017年間不同水果的進(jìn)出口數(shù)量。

            ▲圖2-38 雙向柱狀圖

            5. 瀑布圖

            瀑布圖是由麥肯錫顧問公司所獨(dú)創(chuàng)的圖表類型,因?yàn)樾嗡破俨剂魉Q之為瀑布圖(Waterfall Plot)。此種圖表采用絕對值與相對值結(jié)合的方式,適用于表達(dá)數(shù)個特定數(shù)值之間的數(shù)量變化關(guān)系。圖2-39顯示歷年短跑冠軍的時間跨度,由此可以看出人類體能極限越來越高了。

            ▲圖2-39 瀑布圖

            接下來,我們看看如何用Bokeh依次實(shí)現(xiàn)這些柱狀圖。

            02 實(shí)例

            柱狀圖代碼示例如下所示。

            • 代碼示例 2-27

            1p = figure(plot_width=400, plot_height=400)2p.vbar(x=[1, 2, 3], width=0.5, bottom=0,3 top=[1.2, 2.5, 3.7], color=&#34;red&#34;) # 垂直柱狀圖4show(p) # 顯示

            運(yùn)行結(jié)果如圖2-40所示。

            ▲圖2-40 代碼示例2-27運(yùn)行結(jié)果

            代碼示例2-27第2行采用vbar()方法實(shí)現(xiàn)垂直柱狀圖,該方法具體的參數(shù)說明如下。

            p.vbar(x, width, top, bottom=0, **kwargs)參數(shù)說明。

            • x (:class:`~bokeh.core.properties.NumberSpec` ) : 柱中心x軸坐標(biāo)
            • width (:class:`~bokeh.core.properties.NumberSpec` ) : 柱寬
            • top (:class:`~bokeh.core.properties.NumberSpec` ) : 柱頂部y軸坐標(biāo)
            • bottom (:class:`~bokeh.core.properties.NumberSpec` ) : 柱底部y軸坐標(biāo)
            • 代碼示例 2-28

            1p = figure(plot_width=400, plot_height=400) 2p.hbar(y=[1, 2, 3], height=0.5, left=0, 3 right=[1.2, 2.5, 3.7], color=&#34;navy&#34;) # 水平柱狀圖 4show(p) # 顯示

            運(yùn)行結(jié)果如圖2-41所示。

            ▲圖2-41 代碼示例2-28運(yùn)行結(jié)果

            代碼示例2-28第2行采用hbar()方法實(shí)現(xiàn)橫向柱狀圖,該方法具體的參數(shù)說明如下。

            p.hbar(y, height, right, left=0, **kwargs)參數(shù)說明。

            • y (:class:`~bokeh.core.properties.NumberSpec` ) : 柱中心y軸坐標(biāo)
            • height (:class:`~bokeh.core.properties.NumberSpec` ) :柱的高度(寬度)
            • right (:class:`~bokeh.core.properties.NumberSpec` ) :柱右側(cè)邊界x軸坐標(biāo)
            • left (:class:`~bokeh.core.properties.NumberSpec` ) :柱左側(cè)邊界x軸坐標(biāo)
            • 代碼示例 2-29

            1from bokeh.models import ColumnDataSource 2from bokeh.palettes import Spectral6 3import pandas as pd 4df=pd.read_csv(&#39;data/visualization-20190505.csv&#39;) 5p = figure(x_range=df[&#39;Visualization_tools&#39;],title=&#34;2019年5月常見可視化工具源碼GitHub標(biāo)星數(shù)量&#34;) 6p.vbar(x=df[&#39;Visualization_tools&#39;], top=df[&#39;Star&#39;] , width=0.8, color=Spectral6)7p.xgrid.grid_line_color = None 8p.y_range.start = 0 9show(p)

            運(yùn)行結(jié)果如圖2-42所示。

            ▲圖2-42 代碼示例2-29運(yùn)行結(jié)果

            代碼示例2-29第6行采用vbar()方法展示集中可視化開源工具在GitHub上的Stars數(shù),可以看出Bokeh已經(jīng)超過了Matplotlib。

            • 代碼示例 2-30

            1# 數(shù)據(jù) 2fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 3counts = [5, 3, 4, 2, 4, 6] 4# 畫布 5p = figure(x_range=fruits, plot_height=350, title=&#34;Fruit Counts&#34;, 6# toolbar_location=None, 7# tools=&#34;&#34; 8 ) 9# 柱狀圖 10p.vbar(x=fruits, top=counts, width=0.9) 11# 坐標(biāo)軸設(shè)置 12p.xgrid.grid_line_color = None 13p.y_range.start = 0 14# 顯示 15show(p)

            運(yùn)行結(jié)果如圖2-43所示。

            ▲圖2-43 代碼示例2-30運(yùn)行結(jié)果

            代碼示例2-30第10行采用vbar()方法展示了幾種水果的銷量。

            • 代碼示例 2-31

            1# 數(shù)據(jù) 2fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 3counts = [5, 3, 4, 2, 4, 6] 4# 排序 5sorted_fruits = sorted(fruits, key=lambda x: counts[fruits.index(x)]) 6# 畫布 7p = figure(x_range=sorted_fruits, plot_height=350, title=&#34;Fruit Counts&#34;, 8# toolbar_location=None, tools=&#34;&#34; 9 ) 10# 繪圖 11p.vbar(x=fruits, top=counts, width=0.9) 12# 其他 13p.xgrid.grid_line_color = None 14p.y_range.start = 0 15# 顯示 16show(p)

            運(yùn)行結(jié)果如圖2-44所示。

            ▲圖2-44 代碼示例2-31運(yùn)行結(jié)果

            代碼示例2-31第5行先用sorted()方法對原始數(shù)據(jù)進(jìn)行排序;然后在第11行采用vbar()方法展示了幾種水果的銷量。

            • 代碼示例 2-32

            1from bokeh.models import ColumnDataSource 2from bokeh.palettes import Spectral6 3from bokeh.transform import factor_cmap 4# 數(shù)據(jù) 5fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 6counts = [5, 3, 4, 2, 4, 6] 7source = ColumnDataSource(data=dict(fruits=fruits, counts=counts)) 8# 畫布 9p = figure(x_range=fruits, plot_height=350, toolbar_location=None, title=&#34;Fruit Counts&#34;) 10# 繪圖,分組顏色映射 11p.vbar(x=&#39;fruits&#39;, top=&#39;counts&#39;, width=0.9, source=source, legend=&#34;fruits&#34;, 12 line_color=&#39;white&#39;, fill_color=factor_cmap(&#39;fruits&#39;, palette=Spectral6, factors=fruits)) 13# 坐標(biāo)軸、圖例設(shè)置 14p.xgrid.grid_line_color = None 15p.y_range.start = 0 16p.y_range.end = 9 17p.legend.orientation = &#34;horizontal&#34; 18p.legend.location = &#34;top_center&#34; 19show(p)

            運(yùn)行結(jié)果如圖2-45所示。

            ▲圖2-45 代碼示例2-32運(yùn)行結(jié)果

            代碼示例2-32第11行采用vbar()方法展示了幾種水果的銷量,其中l(wèi)ine_color、fill_color分別為柱的輪廓線顏色和填充顏色,factor_cmap采用數(shù)據(jù)分類進(jìn)行顏色映射。在代碼實(shí)例2-27中,也可以通過color直接定義顏色列表。

            • 代碼示例 2-33

            1from bokeh.models import ColumnDataSource 2from bokeh.palettes import Spectral6 # [&#39;#3288bd&#39;, &#39;#99d594&#39;, &#39;#e6f598&#39;, &#39;#fee08b&#39;, &#39;#fc8d59&#39;, &#39;#d53e4f&#39;] 3fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 4counts = [5, 3, 4, 2, 4, 6] 5source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Spectral6)) 6p = figure(x_range=(0,9), y_range=fruits, plot_height=250, title=&#34;Fruit Counts&#34;, 7# toolbar_location=None, tools=&#34;&#34; 8 ) 9p.hbar(y=&#39;fruits&#39;,left=0,right=&#39;counts&#39;, height=0.5 ,color=&#39;color&#39;, legend=&#34;fruits&#34;, source=source) 10p.xgrid.grid_line_color = None 11# p.legend.orientation = &#34;horizontal&#34; 12p.legend.location = &#34;top_right&#34; 13show(p)

            運(yùn)行結(jié)果如圖2-46所示。

            代碼示例2-33第9行采用hbar()方法展示了幾種水果的銷量,并使用color直接定義顏色列表。

            ▲圖2-46 代碼示例2-33運(yùn)行結(jié)果

            • 代碼示例 2-34

            1fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 2years = [&#39;2015&#39;, &#39;2016&#39;, &#39;2017&#39;] 3data = {&#39;fruits&#39; : fruits, 4 &#39;2015&#39; : [2, 1, 4, 3, 2, 4], 5 &#39;2016&#39; : [5, 3, 3, 2, 4, 6], 6 &#39;2017&#39; : [3, 2, 4, 4, 5, 3]} 7# 創(chuàng)建復(fù)合列表 [ (&#34;Apples&#34;, &#34;2015&#34;), (&#34;Apples&#34;, &#34;2016&#34;), (&#34;Apples&#34;, &#34;2017&#34;), (&#34;Pears&#34;, &#34;2015), ... ] 8x = [ (fruit, year) for fruit in fruits for year in years ] 9counts = sum(zip(data[&#39;2015&#39;], data[&#39;2016&#39;], data[&#39;2017&#39;]), ()) # 分組求和(堆疊總數(shù))10source = ColumnDataSource(data=dict(x=x, counts=counts)) 11# 畫布 12p = figure(x_range=FactorRange(*x), plot_height=350, title=&#34;Fruit Counts by Year&#34;, 13# toolbar_location=None, tools=&#34;&#34; 14 ) 15# 柱狀圖 16p.vbar(x=&#39;x&#39;, top=&#39;counts&#39;, width=0.9, source=source) 17# 其他 18p.y_range.start = 0 19p.x_range.range_padding = 0.1 20p.xaxis.major_label_orientation = 1 21p.xgrid.grid_line_color = None 22# 顯示 23show(p)

            運(yùn)行結(jié)果如圖2-47所示。

            代碼示例2-34第8、9行數(shù)據(jù)預(yù)處理,讀者可以打印數(shù)據(jù)格式;筆者建議在實(shí)踐中多采用Pandas進(jìn)行數(shù)據(jù)預(yù)處理,其DataFrames的復(fù)合序列可以直接作為分組柱狀圖的數(shù)據(jù)。

            ▲圖2-47 代碼示例2-34運(yùn)行結(jié)果

            • 代碼示例 2-35

            1# 數(shù)據(jù) 2fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 3years = [&#39;2015&#39;, &#39;2016&#39;, &#39;2017&#39;] 4data = {&#39;fruits&#39; : fruits, 5 &#39;2015&#39; : [2, 1, 4, 3, 2, 4], 6 &#39;2016&#39; : [5, 3, 3, 2, 4, 6], 7 &#39;2017&#39; : [3, 2, 4, 4, 5, 3]} 8palette = [&#34;#c9d9d3&#34;, &#34;#718dbf&#34;, &#34;#e84d60&#34;] 9x = [ (fruit, year) for fruit in fruits for year in years ] 10counts = sum(zip(data[&#39;2015&#39;], data[&#39;2016&#39;], data[&#39;2017&#39;]), ()) # like an hstack11source = ColumnDataSource(data=dict(x=x, counts=counts)) 12# 畫布 13p = figure(x_range=FactorRange(*x), plot_height=350, title=&#34;Fruit Counts by Year&#34;,14# toolbar_location=None, tools=&#34;&#34; 15 ) 16# 繪圖 17p.vbar(x=&#39;x&#39;, top=&#39;counts&#39;, width=0.9, source=source, line_color=&#34;white&#34;, 18 fill_color=factor_cmap(&#39;x&#39;, palette=palette, factors=years, start=1, end=2))19# 其他 20p.y_range.start = 0 21p.x_range.range_padding = 0.1 22p.xaxis.major_label_orientation = 1 23p.xgrid.grid_line_color = None 24# 顯示 25show(p)

            運(yùn)行結(jié)果如圖2-48所示。

            ▲圖2-48 代碼示例2-35運(yùn)行結(jié)果

            代碼示例2-35在代碼示例2-33的基礎(chǔ)上增加了柱狀圖顏色(第18行),factor_cmap方法是將色板對應(yīng)的顏色列表映射到相應(yīng)的分類數(shù)據(jù)上。

            • 代碼示例 2-36

            1from bokeh.core.properties import value 2from bokeh.transform import dodge 3# 數(shù)據(jù) 4fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 5years = [&#39;2015&#39;, &#39;2016&#39;, &#39;2017&#39;] 6data = {&#39;fruits&#39; : fruits, 7 &#39;2015&#39; : [2, 1, 4, 3, 2, 4], 8 &#39;2016&#39; : [5, 3, 3, 2, 4, 6], 9 &#39;2017&#39; : [3, 2, 4, 4, 5, 3]} 10source = ColumnDataSource(data=data) 11# 畫布 12p = figure(x_range=fruits, y_range=(0, 10), plot_height=350, title=&#34;Fruit Counts by Year&#34;, 13# toolbar_location=None, tools=&#34;&#34; 14 ) 15# 繪圖,采用doge數(shù)據(jù)轉(zhuǎn)換,按產(chǎn)品種類不同年份分組顯示 16p.vbar(x=dodge(&#39;fruits&#39;, -0.25, range=p.x_range), top=&#39;2015&#39;, width=0.2, source=source, 17 color=&#34;#c9d9d3&#34;, legend=value(&#34;2015&#34;)) 1819p.vbar(x=dodge(&#39;fruits&#39;, 0.0, range=p.x_range), top=&#39;2016&#39;, width=0.2, source=source, 20 color=&#34;#718dbf&#34;, legend=value(&#34;2016&#34;)) 2122p.vbar(x=dodge(&#39;fruits&#39;, 0.25, range=p.x_range), top=&#39;2017&#39;, width=0.2, source=source, 23 color=&#34;#e84d60&#34;, legend=value(&#34;2017&#34;)) 24# 其他參數(shù)設(shè)置 25p.x_range.range_padding = 0.1 26p.xgrid.grid_line_color = None 27p.legend.location = &#34;top_left&#34; 28p.legend.orientation = &#34;horizontal&#34; 29# 顯示 30show(p)

            運(yùn)行結(jié)果如圖2-49所示。

            ▲圖2-49 代碼示例2-36運(yùn)行結(jié)果

            代碼示例2-36第16、19、22使用vbar()方法分別繪制2015—2017年各種水果的銷量;其中dodge方法按每年不同種類水果的數(shù)據(jù)分散繪制在x軸范圍內(nèi),是將色板對應(yīng)的顏色列表映射到相應(yīng)的分類數(shù)據(jù)上,dodge第二個參數(shù)表示該分類的起始繪制點(diǎn)。

            • 代碼示例 2-37

            1from bokeh.core.properties import value 2# 數(shù)據(jù) 3fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 4years = [&#34;2015&#34;, &#34;2016&#34;, &#34;2017&#34;] 5colors = [&#34;#c9d9d3&#34;, &#34;#718dbf&#34;, &#34;#e84d60&#34;] 6data = {&#39;fruits&#39; : fruits, 7 &#39;2015&#39; : [2, 1, 4, 3, 2, 4], 8 &#39;2016&#39; : [5, 3, 4, 2, 4, 6], 9 &#39;2017&#39; : [3, 2, 4, 4, 5, 3]} 10# 畫布 11p = figure(x_range=fruits, plot_height=250, title=&#34;Fruit Counts by Year&#34;, 12# toolbar_location=None, 13# tools=&#34;hover&#34;, 14 tooltips=&#34;$name @fruits: @$name&#34;) 15# 繪圖,直接堆疊各年數(shù)據(jù) 16p.vbar_stack(years, x=&#39;fruits&#39;, width=0.9, color=colors, source=data, 17 legend=[value(x) for x in years]) # legend=[{&#39;value&#39;: &#39;2015&#39;}, {&#39;value&#39;: &#39;2016&#39;}, {&#39;value&#39;: &#39;2017&#39;}] 18# 其他 19p.y_range.start = 0 20p.x_range.range_padding = 0.1 21p.xgrid.grid_line_color = None 22p.axis.minor_tick_line_color = None 23p.outline_line_color = None 24p.legend.location = &#34;top_left&#34; 25p.legend.orientation = &#34;horizontal&#34; 26# 顯示 27show(p)

            運(yùn)行結(jié)果如圖2-50所示。

            ▲圖2-50 代碼示例2-37運(yùn)行結(jié)果

            代碼示例2-37第16行使用vbar_stack()方法實(shí)現(xiàn)豎向堆疊柱狀圖,該方法具體的參數(shù)說明如下。

            p.vbar_stack(stackers, **kw)參數(shù)說明。

            • stackers (seq[str]) : 列表,由繪圖數(shù)據(jù)中需要進(jìn)行堆疊的數(shù)據(jù)列名稱組成。

            其他參數(shù)基本上同vbar()方法。

            • 代碼示例 2-38

            1from bokeh.models import Legend 2p = figure(y_range=fruits, plot_height=250,title=&#34;Fruit Counts by Year&#34;, 3# toolbar_location=None, tools=&#34;&#34; 4 ) 5source = ColumnDataSource(data=data) 6p.hbar_stack(years, y=&#39;fruits&#39;,height=0.8, color=colors, source=source, 7 legend=[value(x) for x in years] ) # 堆疊柱狀圖,逐年堆疊 8p.x_range.start = 0 9p.y_range.range_padding = 0.1 # x軸兩側(cè)空白 10p.ygrid.grid_line_color = None 11p.axis.minor_tick_line_color = None 12p.outline_line_color = None 13p.legend.location = &#34;top_right&#34; 14# p.legend.orientation = &#34;horizontal&#34; 15p.legend.click_policy=&#34;hide&#34; 16show(p)

            運(yùn)行結(jié)果如圖2-51所示。

            ▲圖2-51 代碼示例2-38運(yùn)行結(jié)果

            代碼示例2-38第6行使用hbar_stack()方法實(shí)現(xiàn)橫向堆疊柱狀圖,該方法具體的參數(shù)說明如下。

            p.hbar_stack(stackers, **kw)參數(shù)說明。

            • stackers (seq[str]) : 列表,由繪圖數(shù)據(jù)中需要進(jìn)行堆疊的數(shù)據(jù)列名稱組成。

            其他參數(shù)基本上同vbar()方法。

            在學(xué)習(xí)或時間過程中,圖例可能遮蓋圖表,此時可以將圖例移到坐標(biāo)軸外或單獨(dú)作為一個圖層。

            • 代碼示例 2-39

            1from bokeh.palettes import Spectral5 2from bokeh.sampledata.autompg import autompg as df 3from bokeh.transform import factor_cmap 4# 數(shù)據(jù),預(yù)處理 5df.cyl = df.cyl.astype(str) 6group = df.groupby(&#39;cyl&#39;) 7cyl_cmap = factor_cmap(&#39;cyl&#39;, palette=Spectral5, factors=sorted(df.cyl.unique())) # 分組顏色映射 8# 畫布 9p = figure(plot_height=350, x_range=group, title=&#34;MPG by # Cylinders&#34;, 10# toolbar_location=None, tools=&#34;&#34; 11 ) 12# 繪圖 13p.vbar(x=&#39;cyl&#39;, top=&#39;mpg_mean&#39;, width=0.9, source=group, 14 line_color=cyl_cmap, fill_color=cyl_cmap) 15# 其他 16p.y_range.start = 0 17p.xgrid.grid_line_color = None 18p.xaxis.axis_label = &#34;some stuff&#34; 19p.xaxis.major_label_orientation = 1.2 20p.outline_line_color = None 21# 顯示 22show(p)

            運(yùn)行結(jié)果如圖2-52所示。

            ▲圖2-52 代碼示例2-39運(yùn)行結(jié)果

            代碼示例2-39第13行使用vbar()用柱狀圖展示了汽車缸數(shù)與每加侖汽油能行駛的英里數(shù)之間的關(guān)系。

            • 代碼示例 2-40

            1from bokeh.sampledata.autompg import autompg_clean as df 2df.cyl = df.cyl.astype(str) 3df.yr = df.yr.astype(str) 4group = df.groupby([&#39;cyl&#39;, &#39;mfr&#39;]) # 復(fù)合條件分組,[缸數(shù)、廠家] 5index_cmap = factor_cmap(&#39;cyl_mfr&#39;, palette=Spectral5, factors=sorted(df.cyl.unique()), end=1) 6# 畫布 7p = figure(plot_width=800, plot_height=300, title=&#34;Mean MPG by # Cylinders and Manufacturer&#34;, 8 x_range=group, tooltips=[(&#34;MPG&#34;, &#34;@mpg_mean&#34;), (&#34;Cyl, Mfr&#34;, &#34;@cyl_mfr&#34;)]) 9# 繪圖 10p.vbar(x=&#39;cyl_mfr&#39;, top=&#39;mpg_mean&#39;, width=1, source=group, 11 line_color=&#34;white&#34;, fill_color=index_cmap, ) # 尾氣排放量均值 12# 其他 13p.y_range.start = 0 14p.x_range.range_padding = 0.05 # 同css中的padding 15p.xgrid.grid_line_color = None 16p.xaxis.axis_label = &#34;Manufacturer grouped by # Cylinders&#34; 17p.xaxis.major_label_orientation = 1.2 # x軸標(biāo)簽旋轉(zhuǎn) 18p.outline_line_color = None 19# 顯示 20show(p)

            運(yùn)行結(jié)果如圖2-53所示。

            ▲圖2-53 代碼示例2-40運(yùn)行結(jié)果

            代碼示例2-40第10行使用vbar()繪制分組柱狀圖,數(shù)據(jù)分組采用Pandas的groupby方法,該數(shù)據(jù)為復(fù)合序列,展示了汽車缸數(shù)與每加侖汽油能行駛的英里數(shù)之間的關(guān)系。

            • 代碼示例 2-41

            1# 數(shù)據(jù) 2from bokeh.sampledata.sprint import sprint 3sprint.Year = sprint.Year.astype(str) 4group = sprint.groupby(&#39;Year&#39;) 5source = ColumnDataSource(group) 6# 畫布 7p = figure(y_range=group, x_range=(9.5,12.7), plot_width=400, plot_height=550, 8# toolbar_location=None, 9 title=&#34;Time Spreads for Sprint Medalists (by Year)&#34;) 10# 繪圖 11p.hbar(y=&#34;Year&#34;, left=&#39;Time_min&#39;, right=&#39;Time_max&#39;, height=0.4, source=source) # 水平柱狀圖 12# 其他 13p.ygrid.grid_line_color = None 14p.xaxis.axis_label = &#34;Time (seconds)&#34; 15p.outline_line_color = None 16# 顯示 17show(p)

            運(yùn)行結(jié)果如圖2-54所示。

            ▲圖2-54 代碼示例2-41運(yùn)行結(jié)果

            代碼示例2-41第11行使用hbar()繪制瀑布圖,參數(shù)中l(wèi)eft、right為柱左、右坐標(biāo)。若左側(cè)的起始坐標(biāo)均為某一定值,則變回橫向柱狀圖。

            • 代碼示例 2-42

            1from bokeh.core.properties import value 2from bokeh.models import ColumnDataSource, FactorRange 3# 數(shù)據(jù) 4factors = [ 5 (&#34;Q1&#34;, &#34;jan&#34;), (&#34;Q1&#34;, &#34;feb&#34;), (&#34;Q1&#34;, &#34;mar&#34;), 6 (&#34;Q2&#34;, &#34;apr&#34;), (&#34;Q2&#34;, &#34;may&#34;), (&#34;Q2&#34;, &#34;jun&#34;), 7 (&#34;Q3&#34;, &#34;jul&#34;), (&#34;Q3&#34;, &#34;aug&#34;), (&#34;Q3&#34;, &#34;sep&#34;), 8 (&#34;Q4&#34;, &#34;oct&#34;), (&#34;Q4&#34;, &#34;nov&#34;), (&#34;Q4&#34;, &#34;dec&#34;), 910] 11regions = [&#39;east&#39;, &#39;west&#39;] 12source = ColumnDataSource(data=dict( 13 x=factors, 14 east=[ 5, 5, 6, 5, 5, 4, 5, 6, 7, 8, 6, 9 ], 15 west=[ 5, 7, 9, 4, 5, 4, 7, 7, 7, 6, 6, 7 ], 16)) 17# 畫布 18p = figure(x_range=FactorRange(*factors), plot_height=250, 19# toolbar_location=None, tools=&#34;&#34; 20 ) 21# 繪圖 22p.vbar_stack(regions, x=&#39;x&#39;, width=0.9, alpha=0.5, color=[&#34;blue&#34;, &#34;red&#34;], source=source,23 legend=[value(x) for x in regions]) 24# 其他 25p.y_range.start = 0 26p.y_range.end = 18 27p.x_range.range_padding = 0.1 28p.xaxis.major_label_orientation = 1 29p.xgrid.grid_line_color = None 30p.legend.location = &#34;top_center&#34; 31p.legend.orientation = &#34;horizontal&#34; 32# 顯示 33show(p)

            運(yùn)行結(jié)果如圖2-55所示。

            ▲圖2-55 代碼示例2-42運(yùn)行結(jié)果

            代碼示例2-42第18行使用FactorRange ()方法預(yù)定義x軸的范圍(factors的數(shù)據(jù)格式與Pandas復(fù)合序列相似);第19行繪制豎向堆疊柱狀圖。與常規(guī)豎向堆疊柱狀圖相比,該圖采用了復(fù)合序列,多展示了一個維度。

            • 代碼示例 2-43

            1from bokeh.models import ColumnDataSource 2from bokeh.palettes import GnBu3, OrRd3 3# 數(shù)據(jù) 4fruits = [&#39;Apples&#39;, &#39;Pears&#39;, &#39;Nectarines&#39;, &#39;Plums&#39;, &#39;Grapes&#39;, &#39;Strawberries&#39;] 5years = [&#34;2015&#34;, &#34;2016&#34;, &#34;2017&#34;] 6exports = {&#39;fruits&#39; : fruits, 7 &#39;2015&#39; : [2, 1, 4, 3, 2, 4], 8 &#39;2016&#39; : [5, 3, 4, 2, 4, 6], 9 &#39;2017&#39; : [3, 2, 4, 4, 5, 3]} 10imports = {&#39;fruits&#39; : fruits, 11 &#39;2015&#39; : [-1, 0, -1, -3, -2, -1], 12 &#39;2016&#39; : [-2, -1, -3, -1, -2, -2], 13 &#39;2017&#39; : [-1, -2, -1, 0, -2, -2]} 14# 畫布 15p = figure(y_range=fruits, plot_height=350, x_range=(-16, 16), title=&#34;Fruit import/export, by year&#34;, 16# toolbar_location=None 17 ) 18# 水平堆積柱狀圖出口(正向) 19p.hbar_stack(years, y=&#39;fruits&#39;, height=0.9, color=GnBu3, source=ColumnDataSource(exports), 20 legend=[&#34;%s exports&#34; % x for x in years]) 21# 水平堆積柱狀圖進(jìn)口(負(fù)向) 22p.hbar_stack(years, y=&#39;fruits&#39;, height=0.9, color=OrRd3, source=ColumnDataSource(imports), 23 legend=[&#34;%s imports&#34; % x for x in years]) 24# 其他25p.y_range.range_padding = 0.1 26p.ygrid.grid_line_color = None 27p.legend.location = &#34;top_left&#34; 28p.axis.minor_tick_line_color = None 29p.outline_line_color = None 30# 顯示31show(p)

            運(yùn)行結(jié)果如圖2-56所示。

            代碼示例2-43第19、22行分別使用hbar_stack ()方法向左、右兩個方向繪制,實(shí)現(xiàn)橫向堆疊柱狀圖;注意,當(dāng)y軸為分類數(shù)據(jù)(字符串)時,一般需要預(yù)先定義y_range。筆者在實(shí)踐中習(xí)慣用該圖,不受縱向長度約束,適合數(shù)據(jù)較多的長圖,例如全國各省某類型數(shù)據(jù)的比較。

            ▲圖2-56 代碼示例2-43運(yùn)行結(jié)果

            • 代碼示例 2-44

            1from bokeh.models import FactorRange 2factors = [ 3 (&#34;Q1&#34;, &#34;jan&#34;), (&#34;Q1&#34;, &#34;feb&#34;), (&#34;Q1&#34;, &#34;mar&#34;), 4 (&#34;Q2&#34;, &#34;apr&#34;), (&#34;Q2&#34;, &#34;may&#34;), (&#34;Q2&#34;, &#34;jun&#34;), 5 (&#34;Q3&#34;, &#34;jul&#34;), (&#34;Q3&#34;, &#34;aug&#34;), (&#34;Q3&#34;, &#34;sep&#34;), 6 (&#34;Q4&#34;, &#34;oct&#34;), (&#34;Q4&#34;, &#34;nov&#34;), (&#34;Q4&#34;, &#34;dec&#34;), 7 8] # 復(fù)合數(shù)列 9p = figure(x_range=FactorRange(*factors), plot_height=350, 10# toolbar_location=None, tools=&#34;&#34; 11 ) # 如果不采用ColumnDataSource,就必須預(yù)定義factors 12x = [ 10, 12, 16, 9, 10, 8, 12, 13, 14, 14, 12, 16 ] 13# 水平柱狀圖 14p.vbar(x=factors, top=x, width=0.9, alpha=0.5) 15# 折線 16p.line(x=[&#34;Q1&#34;, &#34;Q2&#34;, &#34;Q3&#34;, &#34;Q4&#34;], y=[12, 9, 13, 14], color=&#34;red&#34;, line_width=2)17# 其他 18p.y_range.start = 0 19p.x_range.range_padding = 0.1 20p.xaxis.major_label_orientation = 1 21p.xgrid.grid_line_color = None 22# 顯示 23show(p)

            運(yùn)行結(jié)果如圖2-57所示。

            ▲圖2-57 代碼示例2-44運(yùn)行結(jié)果

            關(guān)于作者:屈希峰,資深Python工程師,Bokeh領(lǐng)域的實(shí)踐者和布道者,對Bokeh有深入的研究。擅長Flask、MongoDB、Sklearn等技術(shù),實(shí)踐經(jīng)驗(yàn)豐富。知乎多個專欄(Python中文社區(qū)、Python程序員、大數(shù)據(jù)分析挖掘)作者,專欄累計(jì)關(guān)注用戶十余萬人。

            本文摘編自《Python數(shù)據(jù)可視化:基于Bokeh的可視化繪圖》,經(jīng)出版方授權(quán)發(fā)布。

            延伸閱讀《Python數(shù)據(jù)可視化》

            推薦語:從圖形繪制、數(shù)據(jù)動態(tài)展示、Web交互等維度全面講解Bokeh功能和使用,不含復(fù)雜數(shù)據(jù)處理和算法,深入淺出,適合零基礎(chǔ)入門,包含大量案例。

            (正文已結(jié)束)

            推薦閱讀:adobe公司

            免責(zé)聲明及提醒:此文內(nèi)容為本網(wǎng)所轉(zhuǎn)載企業(yè)宣傳資訊,該相關(guān)信息僅為宣傳及傳遞更多信息之目的,不代表本網(wǎng)站觀點(diǎn),文章真實(shí)性請瀏覽者慎重核實(shí)!任何投資加盟均有風(fēng)險,提醒廣大民眾投資需謹(jǐn)慎!

            網(wǎng)站簡介 - 聯(lián)系我們 - 營銷服務(wù) - 老版地圖 - 版權(quán)聲明 - 網(wǎng)站地圖
            Copyright.2002-2019 寧夏資訊網(wǎng) 版權(quán)所有 本網(wǎng)拒絕一切非法行為 歡迎監(jiān)督舉報 如有錯誤信息 歡迎糾正
            临清市| 盐边县| 日土县| 华容县| 霸州市| 河西区| 扶绥县| 犍为县| 盱眙县| 奉新县| 南城县| 卢湾区| 界首市| 炉霍县| 灵川县| 同江市| 房产| 陆良县| 阳朔县| 唐山市| 桐柏县| 凤城市| 清水河县| 小金县| 保山市| 和林格尔县| 项城市| 兴安县| 兴城市| 桐梓县| 边坝县| 神池县| 尚义县| 遂宁市| 云南省| 渭源县| 乐都县| 襄垣县| 山西省| 专栏| 绵竹市|