Python使用Bokeh進(jìn)行交互式數(shù)據(jù)可視化
Bokeh是一個(gè)Python庫(kù),用于在Web瀏覽器中創(chuàng)建交互式數(shù)據(jù)可視化。它以一種視覺(jué)上令人愉悅的方式提供了人類(lèi)可讀和快速的數(shù)據(jù)呈現(xiàn)。如果您以前在Python中使用過(guò)可視化,那么您可能使用過(guò)matplotlib。但是Bokeh不同于matplotlib。
要安裝Bokeh,請(qǐng)?jiān)诮K端中輸入以下命令。
pip install bokeh
為什么要使用Bokeh
matplotlib和Bokeh的預(yù)期用途是完全不同的。Matplotlib創(chuàng)建靜態(tài)圖形,這些圖形對(duì)于快速簡(jiǎn)單的可視化或創(chuàng)建出版質(zhì)量的圖像非常有用。Bokeh創(chuàng)建用于在網(wǎng)絡(luò)上顯示的可視化(無(wú)論是本地還是嵌入在網(wǎng)頁(yè)中),最重要的是,可視化意味著高度交互。Matplotlib不提供這兩個(gè)功能。
如果你想與你的數(shù)據(jù)進(jìn)行視覺(jué)交互,或者你想將交互式視覺(jué)數(shù)據(jù)分發(fā)給網(wǎng)絡(luò)觀眾,Bokeh是你的庫(kù)!如果您的主要興趣是生成最終的可視化以供發(fā)布,matplotlib可能更好,盡管Bokeh確實(shí)提供了一種創(chuàng)建靜態(tài)圖形的方法。
繪制一個(gè)簡(jiǎn)單的圖形
前兩個(gè)元素必須分別是x軸和y軸上的數(shù)據(jù)。
color:動(dòng)態(tài)分配顏色,如圖所示。
fill_alpha:指定圓的不透明度。
size:指定每個(gè)圓的大小。
示例
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)
在上面的示例中,output_file()函數(shù)用于將生成的輸出保存為html文件,因?yàn)閎okeh使用web格式來(lái)提供交互式顯示。最后使用show()函數(shù)顯示生成的輸出。
注意事項(xiàng):
紅色= Setosa,綠色= Versicolor,藍(lán)色= Virginica
在每個(gè)可視化的右上角,都有bokeh提供的交互功能。它允許
1.平移圖
2.使用框選擇進(jìn)行縮放
3.使用滾輪縮放
4.保存
5.復(fù)位
6.幫助
繪制條形圖
在這個(gè)例子中,我們將使用自定義創(chuàng)建的數(shù)據(jù)集,使用代碼本身的列表,即水果數(shù)據(jù)集。output_file()函數(shù)用于將生成的輸出保存為html文件,因?yàn)閎okeh使用web格式。我們可以使用ColumnDataSource()函數(shù)將創(chuàng)建的自定義數(shù)據(jù)集(兩個(gè)列表)映射為字典格式。 figure()函數(shù)用于初始化圖形圖形,以便可以在其上繪制數(shù)據(jù),具有各種參數(shù),例如:
- x_range:定義x軸上的數(shù)據(jù)。
- plot_width,plot_height:定義圖形的寬度和高度。
- toolbar_location:定義工具欄的位置。
- title:定義圖的標(biāo)題。
這里我們使用簡(jiǎn)單的豎線來(lái)表示數(shù)據(jù),因此我們使用vbar()方法,并在其中傳遞不同的參數(shù)來(lái)為豎線分配各種屬性,例如:
- x:x軸方向的數(shù)據(jù)
- top:y軸方向的數(shù)據(jù)
- width:定義每個(gè)條形的寬度
- source:數(shù)據(jù)來(lái)源
- legend_field:顯示數(shù)據(jù)中存在的類(lèi)的列表
- line_color:定義圖形中線條的顏色
- fill_color:定義數(shù)據(jù)類(lèi)的不同顏色
最后使用show()函數(shù)顯示生成的輸出。
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)
注意:這是一個(gè)靜態(tài)圖,也是由bokeh提供的,類(lèi)似于matplotlib。
到此這篇關(guān)于Python使用Bokeh進(jìn)行交互式數(shù)據(jù)可視化的文章就介紹到這了,更多相關(guān)Python Bokeh數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Bokeh:Python交互式可視化的利器詳解
- Python?Bokeh實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)可視化
- python使用Bokeh庫(kù)實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)的可視化
- Python使用Bokeh庫(kù)實(shí)現(xiàn)炫目的交互可視化
- Python使用Bokeh實(shí)現(xiàn)交互式圖表的創(chuàng)建
- Python利用Bokeh進(jìn)行數(shù)據(jù)可視化的教程分享
- Python庫(kù)?Bokeh?數(shù)據(jù)可視化實(shí)用指南
- python基于Bokeh庫(kù)制作子彈圖及瀑布圖示例教程
- Python 交互式可視化的利器Bokeh的使用
相關(guān)文章
使用Python進(jìn)行數(shù)獨(dú)求解詳解(二)
對(duì)于利用Python求解數(shù)獨(dú),我們可以采用回溯算法實(shí)現(xiàn)一個(gè)簡(jiǎn)單的版本。本文將此基礎(chǔ)上,通過(guò)改進(jìn)來(lái)提升數(shù)獨(dú)問(wèn)題求解算法的性能。需要的可以參考一下2022-02-02Matlab實(shí)現(xiàn)時(shí)間序列預(yù)測(cè)分類(lèi)實(shí)例代碼
時(shí)間序列是按時(shí)間順序排列的、隨時(shí)間變化且相互關(guān)聯(lián)的數(shù)據(jù)序列,這篇文章主要給大家介紹了關(guān)于Matlab實(shí)現(xiàn)時(shí)間序列預(yù)測(cè)分類(lèi)的相關(guān)資料,需要的朋友可以參考下2021-07-07Python使用jsonpath-rw模塊處理Json對(duì)象操作示例
這篇文章主要介紹了Python使用jsonpath-rw模塊處理Json對(duì)象操作,結(jié)合實(shí)例形式分析了Python使用requests與response處理json的方法,并給出了jsonpath_rw模塊操作json對(duì)象的基本示例,需要的朋友可以參考下2018-07-07Python中typing模塊與類(lèi)型注解的使用方法
這篇文章主要給大家介紹了關(guān)于Python中typing模塊與類(lèi)型注解的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python中創(chuàng)建數(shù)值列表的4種方法總結(jié)
在Python中列表(List)是一種有序、可變的數(shù)據(jù)類(lèi)型,被廣泛用于存儲(chǔ)和處理多個(gè)元素,這篇文章主要給大家介紹了關(guān)于Python中創(chuàng)建數(shù)值列表的4種方法,需要的朋友可以參考下2024-05-05python中dict獲取關(guān)鍵字與值的實(shí)現(xiàn)
這篇文章主要介紹了python中dict獲取關(guān)鍵字與值的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05python神經(jīng)網(wǎng)絡(luò)Keras實(shí)現(xiàn)LSTM及其參數(shù)量詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Keras實(shí)現(xiàn)LSTM及其參數(shù)量詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python3獲取拉勾網(wǎng)招聘信息的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Python3獲取拉勾網(wǎng)招聘信息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04python計(jì)算圓周長(zhǎng)、面積、球體體積并畫(huà)出圓
這篇文章主要介紹了python計(jì)算圓周長(zhǎng)、面積、球體體積并畫(huà)出圓(python3+PyObject+Gtk實(shí)現(xiàn)界面聯(lián)動(dòng)),需要的朋友可以參考下2014-04-04