Bokeh是一個Python庫,用于在Web瀏覽器中創建交互式數據可視化。它以一種視覺上令人愉悅的方式提供了人類可讀和快速的數據呈現。如果您以前在Python中使用過可視化,那么您可能使用過matplotlib。但是Bokeh不同于matplotlib。
要安裝Bokeh,請在終端中輸入以下命令。文章源自四五設計網-http://www.4968ejs.cn/45715.html
pip install bokeh文章源自四五設計網-http://www.4968ejs.cn/45715.html
為什么要使用Bokeh
matplotlib和Bokeh的預期用途是完全不同的。Matplotlib創建靜態圖形,這些圖形對于快速簡單的可視化或創建出版質量的圖像非常有用。Bokeh創建用于在網絡上顯示的可視化(無論是本地還是嵌入在網頁中),最重要的是,可視化意味著高度交互。Matplotlib不提供這兩個功能。文章源自四五設計網-http://www.4968ejs.cn/45715.html
如果你想與你的數據進行視覺交互,或者你想將交互式視覺數據分發給網絡觀眾,Bokeh是你的庫!如果您的主要興趣是生成最終的可視化以供發布,matplotlib可能更好,盡管Bokeh確實提供了一種創建靜態圖形的方法。文章源自四五設計網-http://www.4968ejs.cn/45715.html
繪制一個簡單的圖形
前兩個元素必須分別是x軸和y軸上的數據。文章源自四五設計網-http://www.4968ejs.cn/45715.html
color:動態分配顏色,如圖所示。文章源自四五設計網-http://www.4968ejs.cn/45715.html
fill_alpha:指定圓的不透明度。文章源自四五設計網-http://www.4968ejs.cn/45715.html
size:指定每個圓的大小。文章源自四五設計網-http://www.4968ejs.cn/45715.html
示例文章源自四五設計網-http://www.4968ejs.cn/45715.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from bokeh.plotting import figure, output_file, show from bokeh.sampledata.iris import flowers # assign custom colors to represent each # class of data in a dictionary format colormap = { 'setosa' : 'red' , 'versicolor' : 'green' , ???????????? 'virginica' : 'blue' } colors = [colormap[x] for x in flowers[ 'species' ]] # title for the graph p = figure(title = "Iris Morphology" ) # label on x-axis p.xaxis.axis_label = 'Petal Length' # label on y-axis p.yaxis.axis_label = 'Petal Width' # plot each datapoint as a circle # with custom attributes. p.circle(flowers[ "petal_length" ], ???????? flowers[ "petal_width" ], ???????? color = colors, ???????? fill_alpha = 0.3 , ???????? size = 15 ) # you can save the output as an # interactive html file output_file( "iris1.html" , title = "iris.py example" ) # display the generated plot of graph show(p) |
文章源自四五設計網-http://www.4968ejs.cn/45715.html
在上面的示例中,output_file()函數用于將生成的輸出保存為html文件,因為bokeh使用web格式來提供交互式顯示。最后使用show()函數顯示生成的輸出。
注意事項:
紅色= Setosa,綠色= Versicolor,藍色= Virginica
在每個可視化的右上角,都有bokeh提供的交互功能。它允許
1.平移圖
2.使用框選擇進行縮放
3.使用滾輪縮放
4.保存
5.復位
6.幫助
繪制條形圖
在這個例子中,我們將使用自定義創建的數據集,使用代碼本身的列表,即水果數據集。output_file()函數用于將生成的輸出保存為html文件,因為bokeh使用web格式。我們可以使用ColumnDataSource()函數將創建的自定義數據集(兩個列表)映射為字典格式。 figure()函數用于初始化圖形圖形,以便可以在其上繪制數據,具有各種參數,例如:
- x_range:定義x軸上的數據。
- plot_width,plot_height:定義圖形的寬度和高度。
- toolbar_location:定義工具欄的位置。
- title:定義圖的標題。
這里我們使用簡單的豎線來表示數據,因此我們使用vbar()方法,并在其中傳遞不同的參數來為豎線分配各種屬性,例如:
- x:x軸方向的數據
- top:y軸方向的數據
- width:定義每個條形的寬度
- source:數據來源
- legend_field:顯示數據中存在的類的列表
- line_color:定義圖形中線條的顏色
- fill_color:定義數據類的不同顏色
最后使用show()函數顯示生成的輸出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | from bokeh.io import output_file, show from bokeh.models import ColumnDataSource from bokeh.palettes import Spectral10 from bokeh.plotting import figure from bokeh.transform import factor_cmap output_file( "fruits_bar_chart.html" ) #output save file name # creating custom data fruits = [ 'Apples' , 'Pears' , 'Nectarines' , ???????? 'Plums' , 'Grapes' , 'Strawberries' , ???????? 'bananas' , 'berries' , 'pineapples' , 'litchi' ] counts = [ 51 , 34 , 4 , 28 , 119 , 79 , 15 , 68 , 26 , 88 ] # mapping counts with classes as a dictionary source = ColumnDataSource(data = dict (fruits = fruits, ???????????????????????????????????? counts = counts)) # initializing the figure p = figure(x_range = fruits, ???????? plot_width = 800 , ???????? plot_height = 350 , ???????? toolbar_location = None , ???????? title = "Fruit Counts" ) # assigning various attributes to plot p.vbar(x = 'fruits' , top = 'counts' , ???? width = 1 , source = source, ???? legend_field = "fruits" , ???? line_color = 'white' , ???? fill_color = factor_cmap( 'fruits' , ???????????????????????????? palette = Spectral10, ???????????????????????????? factors = fruits)) p.xgrid.grid_line_color = None p.y_range.start = 0 p.y_range.end = 150 p.legend.orientation = "horizontal" p.legend.location = "top_center" # display output show(p) |
注意:這是一個靜態圖,也是由bokeh提供的,類似于matplotlib。
到此這篇關于Python使用Bokeh進行交互式數據可視化的文章就介紹到這了


評論