Bokeh是一個(gè)Python庫,用于在Web瀏覽器中創(chuàng)建交互式數(shù)據(jù)可視化。它以一種視覺上令人愉悅的方式提供了人類可讀和快速的數(shù)據(jù)呈現(xiàn)。如果您以前在Python中使用過可視化,那么您可能使用過matplotlib。但是Bokeh不同于matplotlib。
要安裝Bokeh,請(qǐng)?jiān)诮K端中輸入以下命令。文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
pip install bokeh文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
為什么要使用Bokeh
matplotlib和Bokeh的預(yù)期用途是完全不同的。Matplotlib創(chuàng)建靜態(tài)圖形,這些圖形對(duì)于快速簡(jiǎn)單的可視化或創(chuàng)建出版質(zhì)量的圖像非常有用。Bokeh創(chuàng)建用于在網(wǎng)絡(luò)上顯示的可視化(無論是本地還是嵌入在網(wǎng)頁中),最重要的是,可視化意味著高度交互。Matplotlib不提供這兩個(gè)功能。文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
如果你想與你的數(shù)據(jù)進(jìn)行視覺交互,或者你想將交互式視覺數(shù)據(jù)分發(fā)給網(wǎng)絡(luò)觀眾,Bokeh是你的庫!如果您的主要興趣是生成最終的可視化以供發(fā)布,matplotlib可能更好,盡管Bokeh確實(shí)提供了一種創(chuàng)建靜態(tài)圖形的方法。文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
繪制一個(gè)簡(jiǎn)單的圖形
前兩個(gè)元素必須分別是x軸和y軸上的數(shù)據(jù)。文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
color:動(dòng)態(tài)分配顏色,如圖所示。文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
fill_alpha:指定圓的不透明度。文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
size:指定每個(gè)圓的大小。文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
示例文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/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, showfrom bokeh.sampledata.iris import flowers# assign custom colors to represent each# class of data in a dictionary formatcolormap = {'setosa': 'red', 'versicolor': 'green',????????????'virginica': 'blue'}colors = [colormap[x] for x in flowers['species']]# title for the graphp = figure(title="Iris Morphology")# label on x-axisp.xaxis.axis_label = 'Petal Length'# label on y-axisp.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 fileoutput_file("iris1.html", title="iris.py example")# display the generated plot of graphshow(p) |
文章源自四五設(shè)計(jì)網(wǎng)-http://m.wasochina.com/45715.html
在上面的示例中,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)題。
這里我們使用簡(jiǎn)單的豎線來表示數(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ù)顯示生成的輸出。
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, showfrom bokeh.models import ColumnDataSourcefrom bokeh.palettes import Spectral10from bokeh.plotting import figurefrom bokeh.transform import factor_cmapoutput_file("fruits_bar_chart.html") #output save file name# creating custom datafruits = ['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 dictionarysource = ColumnDataSource(data=dict(fruits=fruits,????????????????????????????????????counts=counts))# initializing the figurep = figure(x_range=fruits,????????plot_width=800,????????plot_height=350,????????toolbar_location=None,????????title="Fruit Counts")# assigning various attributes to plotp.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 = Nonep.y_range.start = 0p.y_range.end = 150p.legend.orientation = "horizontal"p.legend.location = "top_center"# display outputshow(p) |

注意:這是一個(gè)靜態(tài)圖,也是由bokeh提供的,類似于matplotlib。
到此這篇關(guān)于Python使用Bokeh進(jìn)行交互式數(shù)據(jù)可視化的文章就介紹到這了



評(píng)論