Python使用Bokeh進(jìn)行交互式數(shù)據(jù)可視化
Bokeh是一個(gè)Python庫,用于在Web瀏覽器中創(chuàng)建交互式數(shù)據(jù)可視化。它以一種視覺上令人愉悅的方式提供了人類可讀和快速的數(shù)據(jù)呈現(xiàn)。如果您以前在Python中使用過可視化,那么您可能使用過matplotlib。但是Bokeh不同于matplotlib。
要安裝Bokeh,請(qǐng)?jiān)诮K端中輸入以下命令。
pip install bokeh
為什么要使用Bokeh
matplotlib和Bokeh的預(yù)期用途是完全不同的。Matplotlib創(chuàng)建靜態(tài)圖形,這些圖形對(duì)于快速簡單的可視化或創(chuàng)建出版質(zhì)量的圖像非常有用。Bokeh創(chuàng)建用于在網(wǎng)絡(luò)上顯示的可視化(無論是本地還是嵌入在網(wǎng)頁中),最重要的是,可視化意味著高度交互。Matplotlib不提供這兩個(gè)功能。
如果你想與你的數(shù)據(jù)進(jìn)行視覺交互,或者你想將交互式視覺數(shù)據(jù)分發(fā)給網(wǎng)絡(luò)觀眾,Bokeh是你的庫!如果您的主要興趣是生成最終的可視化以供發(fā)布,matplotlib可能更好,盡管Bokeh確實(shí)提供了一種創(chuàng)建靜態(tài)圖形的方法。
繪制一個(gè)簡單的圖形
前兩個(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格式來提供交互式顯示。最后使用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)題。
這里我們使用簡單的豎線來表示數(shù)據(jù),因此我們使用vbar()方法,并在其中傳遞不同的參數(shù)來為豎線分配各種屬性,例如:
- x:x軸方向的數(shù)據(jù)
- top:y軸方向的數(shù)據(jù)
- width:定義每個(gè)條形的寬度
- source:數(shù)據(jù)來源
- legend_field:顯示數(shù)據(jù)中存在的類的列表
- line_color:定義圖形中線條的顏色
- fill_color:定義數(shù)據(jù)類的不同顏色
最后使用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提供的,類似于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庫實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)的可視化
- Python使用Bokeh庫實(shí)現(xiàn)炫目的交互可視化
- Python使用Bokeh實(shí)現(xiàn)交互式圖表的創(chuàng)建
- Python利用Bokeh進(jìn)行數(shù)據(jù)可視化的教程分享
- Python庫?Bokeh?數(shù)據(jù)可視化實(shí)用指南
- python基于Bokeh庫制作子彈圖及瀑布圖示例教程
- Python 交互式可視化的利器Bokeh的使用
相關(guān)文章
Python深度強(qiáng)化學(xué)習(xí)之DQN算法原理詳解
DQN算法是DeepMind團(tuán)隊(duì)提出的一種深度強(qiáng)化學(xué)習(xí)算法,在許多電動(dòng)游戲中達(dá)到人類玩家甚至超越人類玩家的水準(zhǔn),本文就帶領(lǐng)大家了解一下這個(gè)算法,快來跟隨小編學(xué)習(xí)一下2021-12-12
解決導(dǎo)入django_filters不成功問題No module named ''django_filter''
這篇文章主要介紹了解決導(dǎo)入django_filters不成功問題No module named 'django_filter',具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
python+selenium對(duì)table表和分頁處理
這篇文章主要介紹了python+selenium對(duì)table表和分頁處理,文章內(nèi)容只要包括bulabula2022、table表分頁處理、網(wǎng)頁table所有內(nèi)容循環(huán)處理等相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-01-01
基于python實(shí)現(xiàn)復(fù)制文件并重命名
這篇文章主要介紹了基于python實(shí)現(xiàn)復(fù)制文件并重命名,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
python使用ctypes模塊調(diào)用windowsapi獲取系統(tǒng)版本示例
這篇文章主要介紹了python使用ctypes模塊調(diào)用windowsapi獲取系統(tǒng)版本示例,需要的朋友可以參考下2014-04-04
詳解Python中的整除運(yùn)算及其應(yīng)用場景
在Python編程中,整除運(yùn)算(也稱為整商運(yùn)算)是一個(gè)常見的操作,它用于計(jì)算兩個(gè)數(shù)相除后的整數(shù)部分,下面就跟隨小編一起來了解一下它的語法,工作原理和實(shí)際應(yīng)用案例吧2024-11-11
基于wxPython的GUI實(shí)現(xiàn)輸入對(duì)話框(2)
這篇文章主要為大家詳細(xì)介紹了基于wxPython的GUI實(shí)現(xiàn)輸入對(duì)話框的第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Python機(jī)器學(xué)習(xí)之基于Pytorch實(shí)現(xiàn)貓狗分類
看了許多關(guān)于PyTorch的入門文章,大抵是從torchvision.datasets中自帶的數(shù)據(jù)集進(jìn)行訓(xùn)練,導(dǎo)致很難把PyTorch運(yùn)用于自己的數(shù)據(jù)集上,真正地靈活運(yùn)用PyTorch,本文詳細(xì)介紹了怎么利用Pytorch實(shí)現(xiàn)貓狗分類,需要的朋友可以參考下2021-06-06

