01 概述css
柱狀圖(Histogram)是一種以長方形的長度爲變量的表達圖形的統計報告圖,由一系列高度不等的縱向條紋表示數據分佈的狀況,用來比較兩個或兩個以上的價值(不一樣時間或者不一樣條件),只有一個變量,一般用於較小的數據集分析。app
柱狀圖也可橫向排列,或用多維方式表達。其主要用於數據統計與分析,早期主要用於數學統計學科中,用柱狀圖表示數碼相機的曝光值,到現代使用已經比較普遍,好比現代的電子產品和一些軟件的分析測試,如電腦、數碼相機的顯示器和Photoshop上都能看到相應的柱狀圖。ide
1. 基礎柱狀圖工具
基礎柱狀圖常常用來對比數值的大小,使用範圍很是普遍,例如科比在不一樣賽季的得分、不一樣遊戲App下載量、不一樣時期手機端綜合搜索用戶規模等,圖2-33顯示不一樣種類水果的銷量。學習
▲圖2-33 基本柱狀圖測試
須要注意的是,分類太多不適合使用豎向柱狀圖,如圖2-34所示。ui
▲圖2-34 豎向柱狀圖spa
此時,須要用到橫向柱狀圖,如圖2-35所示。3d
▲圖2-35 橫向柱狀圖code
2. 分組柱狀圖
分組柱狀圖,又叫聚合柱狀圖。當使用者須要在同一個軸上顯示各個分類下不一樣的分組時,須要用到分組柱狀圖。
跟柱狀圖相似,使用柱子的高度來映射和對比數據值。每一個分組中的柱子使用不一樣顏色或者相同顏色不一樣透明的方式區別各個分類,各個分組之間須要保持間隔。
分組柱狀圖常常用於不一樣組間數據的比較,這些組都包含了相同分類的數據。例如,展現改革開放以來城鎮與農村人口的變化,不一樣遊戲公司的休閒、益智、格鬥類App的下載量對比等。圖2-36對比了2015—2017年間不一樣水果的銷量。
▲圖2-36 分組柱狀圖
3. 堆疊柱狀圖
與並排顯示分類的分組柱狀圖不一樣,堆疊柱狀圖將每一個柱子進行分割以顯示相同類型下各個數據的大小狀況。它能夠形象地展現一個大分類包含的每一個小分類的數據,以及各個小分類的佔比,顯示的是單個項目與總體之間的關係。咱們將堆疊柱狀圖分爲兩種類型:
1)通常的堆疊柱狀圖:每一根柱子上的值分別表明不一樣的數據大小,各層的數據總和表明整根柱子的高度。很是適用於比較每一個分組的數據總量。
2)百分比的堆疊柱狀圖:柱子的各個層表明的是該類別數據佔該分組整體數據的百分比。
堆疊柱狀圖的一個缺點是當柱子上的堆疊太多時會致使數據很難區分對比,同時很難對比不一樣分類下相同維度的數據,由於它們不是按照同一基準線對齊的。
圖2-37是顯示2015—2017年間不一樣水果的累計數量。
▲圖2-37 堆疊柱狀圖
4. 雙向柱狀圖
雙向柱狀圖,又名正負條形圖,使用正向和反向的柱子顯示類別之間的數值比較。其中分類軸表示須要對比的分類維度,連續軸表明相應的數值,分爲兩種狀況,一種是正向刻度值與反向刻度值徹底對稱,另外一種是正向刻度值與反向刻度值反向對稱,即互爲相反數。
圖2-38是顯示2015—2017年間不一樣水果的進出口數量。
▲圖2-38 雙向柱狀圖
5. 瀑布圖
瀑布圖是由麥肯錫顧問公司所首創的圖表類型,由於形似瀑布流水而稱之爲瀑布圖(Waterfall Plot)。此種圖表採用絕對值與相對值結合的方式,適用於表達數個特定數值之間的數量變化關係。圖2-39顯示歷年短跑冠軍的時間跨度,由此能夠看出人類體能極限愈來愈高了。
▲圖2-39 瀑布圖
接下來,咱們看看如何用Bokeh依次實現這些柱狀圖。
02 實例
柱狀圖代碼示例以下所示。
代碼示例 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="red") # 垂直柱狀圖
4show(p) # 顯示
運行結果如圖2-40所示。
▲圖2-40 代碼示例2-27運行結果
代碼示例2-27第2行採用vbar()方法實現垂直柱狀圖,該方法具體的參數說明以下。
p.vbar(x, width, top, bottom=0, **kwargs)參數說明。
x (:class:`~bokeh.core.properties.NumberSpec` ) : 柱中心x軸座標
width (:class:`~bokeh.core.properties.NumberSpec` ) : 柱寬
top (:class:`~bokeh.core.properties.NumberSpec` ) : 柱頂部y軸座標
bottom (:class:`~bokeh.core.properties.NumberSpec` ) : 柱底部y軸座標
代碼示例 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="navy") # 水平柱狀圖
4show(p) # 顯示
運行結果如圖2-41所示。
▲圖2-41 代碼示例2-28運行結果
代碼示例2-28第2行採用hbar()方法實現橫向柱狀圖,該方法具體的參數說明以下。
p.hbar(y, height, right, left=0, **kwargs)參數說明。
y (:class:`~bokeh.core.properties.NumberSpec` ) : 柱中心y軸座標
height (:class:`~bokeh.core.properties.NumberSpec` ) :柱的高度(寬度)
right (:class:`~bokeh.core.properties.NumberSpec` ) :柱右側邊界x軸座標
left (:class:`~bokeh.core.properties.NumberSpec` ) :柱左側邊界x軸座標
代碼示例 2-29
1from bokeh.models import ColumnDataSource
2from bokeh.palettes import Spectral6
3import pandas as pd
4df=pd.read_csv('data/visualization-20190505.csv')
5p = figure(x_range=df['Visualization_tools'],title="2019年5月常見可視化工具源碼GitHub標星數量")
6p.vbar(x=df['Visualization_tools'], top=df['Star'] , width=0.8, color=Spectral6)
7p.xgrid.grid_line_color = None
8p.y_range.start = 0
9show(p)
運行結果如圖2-42所示。
▲圖2-42 代碼示例2-29運行結果
代碼示例2-29第6行採用vbar()方法展現集中可視化開源工具在GitHub上的Stars數,能夠看出Bokeh已經超過了Matplotlib。
代碼示例 2-30
1# 數據
2fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
3counts = [5, 3, 4, 2, 4, 6]
4# 畫布
5p = figure(x_range=fruits, plot_height=350, title="Fruit Counts",
6# toolbar_location=None,
7# tools=""
8 )
9# 柱狀圖
10p.vbar(x=fruits, top=counts, width=0.9)
11# 座標軸設置
12p.xgrid.grid_line_color = None
13p.y_range.start = 0
14# 顯示
15show(p)
運行結果如圖2-43所示。
▲圖2-43 代碼示例2-30運行結果
代碼示例2-30第10行採用vbar()方法展現了幾種水果的銷量。
代碼示例 2-31
1# 數據
2fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
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="Fruit Counts",
8# toolbar_location=None, tools=""
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)
運行結果如圖2-44所示。
▲圖2-44 代碼示例2-31運行結果
代碼示例2-31第5行先用sorted()方法對原始數據進行排序;而後在第11行採用vbar()方法展現了幾種水果的銷量。
代碼示例 2-32
1from bokeh.models import ColumnDataSource
2from bokeh.palettes import Spectral6
3from bokeh.transform import factor_cmap
4# 數據
5fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
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="Fruit Counts")
10# 繪圖,分組顏色映射
11p.vbar(x='fruits', top='counts', width=0.9, source=source, legend="fruits",
12 line_color='white', fill_color=factor_cmap('fruits', palette=Spectral6, factors=fruits))
13# 座標軸、圖例設置
14p.xgrid.grid_line_color = None
15p.y_range.start = 0
16p.y_range.end = 9
17p.legend.orientation = "horizontal"
18p.legend.location = "top_center"
19show(p)
運行結果如圖2-45所示。
▲圖2-45 代碼示例2-32運行結果
代碼示例2-32第11行採用vbar()方法展現了幾種水果的銷量,其中line_color、fill_color分別爲柱的輪廓線顏色和填充顏色,factor_cmap採用數據分類進行顏色映射。在代碼實例2-27中,也能夠經過color直接定義顏色列表。
代碼示例 2-33
1from bokeh.models import ColumnDataSource
2from bokeh.palettes import Spectral6 # ['#3288bd', '#99d594', '#e6f598', '#fee08b', '#fc8d59', '#d53e4f']
3fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
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="Fruit Counts",
7# toolbar_location=None, tools=""
8 )
9p.hbar(y='fruits',left=0,right='counts', height=0.5 ,color='color', legend="fruits", source=source)
10p.xgrid.grid_line_color = None
11# p.legend.orientation = "horizontal"
12p.legend.location = "top_right"
13show(p)
運行結果如圖2-46所示。
代碼示例2-33第9行採用hbar()方法展現了幾種水果的銷量,並使用color直接定義顏色列表。
▲圖2-46 代碼示例2-33運行結果
代碼示例 2-34
1fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
2years = ['2015', '2016', '2017']
3data = {'fruits' : fruits,
4 '2015' : [2, 1, 4, 3, 2, 4],
5 '2016' : [5, 3, 3, 2, 4, 6],
6 '2017' : [3, 2, 4, 4, 5, 3]}
7# 建立複合列表 [ ("Apples", "2015"), ("Apples", "2016"), ("Apples", "2017"), ("Pears", "2015), ... ]
8x = [ (fruit, year) for fruit in fruits for year in years ]
9counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # 分組求和(堆疊總數)
10source = ColumnDataSource(data=dict(x=x, counts=counts))
11# 畫布
12p = figure(x_range=FactorRange(*x), plot_height=350, title="Fruit Counts by Year",
13# toolbar_location=None, tools=""
14 )
15# 柱狀圖
16p.vbar(x='x', top='counts', 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)
運行結果如圖2-47所示。
代碼示例2-34第八、9行數據預處理,讀者能夠打印數據格式;筆者建議在實踐中多采用Pandas進行數據預處理,其DataFrames的複合序列能夠直接做爲分組柱狀圖的數據。
▲圖2-47 代碼示例2-34運行結果
代碼示例 2-35
1# 數據
2fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
3years = ['2015', '2016', '2017']
4data = {'fruits' : fruits,
5 '2015' : [2, 1, 4, 3, 2, 4],
6 '2016' : [5, 3, 3, 2, 4, 6],
7 '2017' : [3, 2, 4, 4, 5, 3]}
8palette = ["#c9d9d3", "#718dbf", "#e84d60"]
9x = [ (fruit, year) for fruit in fruits for year in years ]
10counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # like an hstack
11source = ColumnDataSource(data=dict(x=x, counts=counts))
12# 畫布
13p = figure(x_range=FactorRange(*x), plot_height=350, title="Fruit Counts by Year",
14# toolbar_location=None, tools=""
15 )
16# 繪圖
17p.vbar(x='x', top='counts', width=0.9, source=source, line_color="white",
18 fill_color=factor_cmap('x', 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)
運行結果如圖2-48所示。
▲圖2-48 代碼示例2-35運行結果
代碼示例2-35在代碼示例2-33的基礎上增長了柱狀圖顏色(第18行),factor_cmap方法是將色板對應的顏色列表映射到相應的分類數據上。
代碼示例 2-36
1from bokeh.core.properties import value
2from bokeh.transform import dodge
3# 數據
4fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
5years = ['2015', '2016', '2017']
6data = {'fruits' : fruits,
7 '2015' : [2, 1, 4, 3, 2, 4],
8 '2016' : [5, 3, 3, 2, 4, 6],
9 '2017' : [3, 2, 4, 4, 5, 3]}
10source = ColumnDataSource(data=data)
11# 畫布
12p = figure(x_range=fruits, y_range=(0, 10), plot_height=350, title="Fruit Counts by Year",
13# toolbar_location=None, tools=""
14 )
15# 繪圖,採用doge數據轉換,按產品種類不一樣年份分組顯示
16p.vbar(x=dodge('fruits', -0.25, range=p.x_range), top='2015', width=0.2, source=source,
17 color="#c9d9d3", legend=value("2015"))
18
19p.vbar(x=dodge('fruits', 0.0, range=p.x_range), top='2016', width=0.2, source=source,
20 color="#718dbf", legend=value("2016"))
21
22p.vbar(x=dodge('fruits', 0.25, range=p.x_range), top='2017', width=0.2, source=source,
23 color="#e84d60", legend=value("2017"))
24# 其餘參數設置
25p.x_range.range_padding = 0.1
26p.xgrid.grid_line_color = None
27p.legend.location = "top_left"
28p.legend.orientation = "horizontal"
29# 顯示
30show(p)
運行結果如圖2-49所示。
▲圖2-49 代碼示例2-36運行結果
代碼示例2-36第1六、1九、22使用vbar()方法分別繪製2015—2017年各類水果的銷量;其中dodge方法按每一年不一樣種類水果的數據分散繪製在x軸範圍內,是將色板對應的顏色列表映射到相應的分類數據上,dodge第二個參數表示該分類的起始繪製點。
代碼示例 2-37
1from bokeh.core.properties import value
2# 數據
3fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
4years = ["2015", "2016", "2017"]
5colors = ["#c9d9d3", "#718dbf", "#e84d60"]
6data = {'fruits' : fruits,
7 '2015' : [2, 1, 4, 3, 2, 4],
8 '2016' : [5, 3, 4, 2, 4, 6],
9 '2017' : [3, 2, 4, 4, 5, 3]}
10# 畫布
11p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
12# toolbar_location=None,
13# tools="hover",
14 tooltips="$name @fruits: @$name")
15# 繪圖,直接堆疊各年數據
16p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data,
17 legend=[value(x) for x in years]) # legend=[{'value': '2015'}, {'value': '2016'}, {'value': '2017'}]
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 = "top_left"
25p.legend.orientation = "horizontal"
26# 顯示
27show(p)
運行結果如圖2-50所示。
▲圖2-50 代碼示例2-37運行結果
代碼示例2-37第16行使用vbar_stack()方法實現豎向堆疊柱狀圖,該方法具體的參數說明以下。
p.vbar_stack(stackers, **kw)參數說明。
stackers (seq[str]) : 列表,由繪圖數據中須要進行堆疊的數據列名稱組成。
其餘參數基本上同vbar()方法。
代碼示例 2-38
1from bokeh.models import Legend
2p = figure(y_range=fruits, plot_height=250,title="Fruit Counts by Year",
3# toolbar_location=None, tools=""
4 )
5source = ColumnDataSource(data=data)
6p.hbar_stack(years, y='fruits',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軸兩側空白
10p.ygrid.grid_line_color = None
11p.axis.minor_tick_line_color = None
12p.outline_line_color = None
13p.legend.location = "top_right"
14# p.legend.orientation = "horizontal"
15p.legend.click_policy="hide"
16show(p)
運行結果如圖2-51所示。
▲圖2-51 代碼示例2-38運行結果
代碼示例2-38第6行使用hbar_stack()方法實現橫向堆疊柱狀圖,該方法具體的參數說明以下。
p.hbar_stack(stackers, **kw)參數說明。
stackers (seq[str]) : 列表,由繪圖數據中須要進行堆疊的數據列名稱組成。
其餘參數基本上同vbar()方法。
在學習或時間過程當中,圖例可能遮蓋圖表,此時能夠將圖例移到座標軸外或單獨做爲一個圖層。
代碼示例 2-39
1from bokeh.palettes import Spectral5
2from bokeh.sampledata.autompg import autompg as df
3from bokeh.transform import factor_cmap
4# 數據,預處理
5df.cyl = df.cyl.astype(str)
6group = df.groupby('cyl')
7cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique())) # 分組顏色映射
8# 畫布
9p = figure(plot_height=350, x_range=group, title="MPG by # Cylinders",
10# toolbar_location=None, tools=""
11 )
12# 繪圖
13p.vbar(x='cyl', top='mpg_mean', 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 = "some stuff"
19p.xaxis.major_label_orientation = 1.2
20p.outline_line_color = None
21# 顯示
22show(p)
運行結果如圖2-52所示。
▲圖2-52 代碼示例2-39運行結果
代碼示例2-39第13行使用vbar()用柱狀圖展現了汽車缸數與每加侖汽油能行駛的英里數之間的關係。
代碼示例 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(['cyl', 'mfr']) # 複合條件分組,[缸數、廠家]
5index_cmap = factor_cmap('cyl_mfr', palette=Spectral5, factors=sorted(df.cyl.unique()), end=1)
6# 畫布
7p = figure(plot_width=800, plot_height=300, title="Mean MPG by # Cylinders and Manufacturer",
8 x_range=group, tooltips=[("MPG", "@mpg_mean"), ("Cyl, Mfr", "@cyl_mfr")])
9# 繪圖
10p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=group,
11 line_color="white", 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 = "Manufacturer grouped by # Cylinders"
17p.xaxis.major_label_orientation = 1.2 # x軸標籤旋轉
18p.outline_line_color = None
19# 顯示
20show(p)
運行結果如圖2-53所示。
▲圖2-53 代碼示例2-40運行結果
代碼示例2-40第10行使用vbar()繪製分組柱狀圖,數據分組採用Pandas的groupby方法,該數據爲複合序列,展現了汽車缸數與每加侖汽油能行駛的英里數之間的關係。
代碼示例 2-41
1# 數據
2from bokeh.sampledata.sprint import sprint
3sprint.Year = sprint.Year.astype(str)
4group = sprint.groupby('Year')
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="Time Spreads for Sprint Medalists (by Year)")
10# 繪圖
11p.hbar(y="Year", left='Time_min', right='Time_max', height=0.4, source=source) # 水平柱狀圖
12# 其餘
13p.ygrid.grid_line_color = None
14p.xaxis.axis_label = "Time (seconds)"
15p.outline_line_color = None
16# 顯示
17show(p)
運行結果如圖2-54所示。
▲圖2-54 代碼示例2-41運行結果
代碼示例2-41第11行使用hbar()繪製瀑布圖,參數中left、right爲柱左、右座標。若左側的起始座標均爲某必定值,則變回橫向柱狀圖。
代碼示例 2-42
1from bokeh.core.properties import value
2from bokeh.models import ColumnDataSource, FactorRange
3# 數據
4factors = [
5 ("Q1", "jan"), ("Q1", "feb"), ("Q1", "mar"),
6 ("Q2", "apr"), ("Q2", "may"), ("Q2", "jun"),
7 ("Q3", "jul"), ("Q3", "aug"), ("Q3", "sep"),
8 ("Q4", "oct"), ("Q4", "nov"), ("Q4", "dec"),
9
10]
11regions = ['east', 'west']
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=""
20 )
21# 繪圖
22p.vbar_stack(regions, x='x', width=0.9, alpha=0.5, color=["blue", "red"], 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 = "top_center"
31p.legend.orientation = "horizontal"
32# 顯示
33show(p)
運行結果如圖2-55所示。
▲圖2-55 代碼示例2-42運行結果
代碼示例2-42第18行使用FactorRange ()方法預約義x軸的範圍(factors的數據格式與Pandas複合序列類似);第19行繪製豎向堆疊柱狀圖。與常規豎向堆疊柱狀圖相比,該圖採用了複合序列,多展現了一個維度。
代碼示例 2-43
1from bokeh.models import ColumnDataSource
2from bokeh.palettes import GnBu3, OrRd3
3# 數據
4fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
5years = ["2015", "2016", "2017"]
6exports = {'fruits' : fruits,
7 '2015' : [2, 1, 4, 3, 2, 4],
8 '2016' : [5, 3, 4, 2, 4, 6],
9 '2017' : [3, 2, 4, 4, 5, 3]}
10imports = {'fruits' : fruits,
11 '2015' : [-1, 0, -1, -3, -2, -1],
12 '2016' : [-2, -1, -3, -1, -2, -2],
13 '2017' : [-1, -2, -1, 0, -2, -2]}
14# 畫布
15p = figure(y_range=fruits, plot_height=350, x_range=(-16, 16), title="Fruit import/export, by year",
16# toolbar_location=None
17 )
18# 水平堆積柱狀圖出口(正向)
19p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(exports),
20 legend=["%s exports" % x for x in years])
21# 水平堆積柱狀圖進口(負向)
22p.hbar_stack(years, y='fruits', height=0.9, color=OrRd3, source=ColumnDataSource(imports),
23 legend=["%s imports" % x for x in years])
24# 其餘
25p.y_range.range_padding = 0.1
26p.ygrid.grid_line_color = None
27p.legend.location = "top_left"
28p.axis.minor_tick_line_color = None
29p.outline_line_color = None
30# 顯示
31show(p)
運行結果如圖2-56所示。
代碼示例2-43第1九、22行分別使用hbar_stack ()方法向左、右兩個方向繪製,實現橫向堆疊柱狀圖;注意,當y軸爲分類數據(字符串)時,通常須要預先定義y_range。筆者在實踐中習慣用該圖,不受縱向長度約束,適合數據較多的長圖,例如全國各省某類型數據的比較。
▲圖2-56 代碼示例2-43運行結果
代碼示例 2-44
1from bokeh.models import FactorRange
2factors = [
3 ("Q1", "jan"), ("Q1", "feb"), ("Q1", "mar"),
4 ("Q2", "apr"), ("Q2", "may"), ("Q2", "jun"),
5 ("Q3", "jul"), ("Q3", "aug"), ("Q3", "sep"),
6 ("Q4", "oct"), ("Q4", "nov"), ("Q4", "dec"),
7
8] # 複合數列
9p = figure(x_range=FactorRange(*factors), plot_height=350,
10# toolbar_location=None, tools=""
11 ) # 若是不採用ColumnDataSource,就必須預約義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=["Q1", "Q2", "Q3", "Q4"], y=[12, 9, 13, 14], color="red", 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)
運行結果如圖2-57所示。
▲圖2-57 代碼示例2-44運行結果