Python 數(shù)據(jù)可視化之Bokeh詳解
安裝
要安裝此類型,請在終端中輸入以下命令。
pip install bokeh
散點圖
散點圖中散景可以使用繪圖模塊的散射()方法被繪制。這里分別傳遞 x 和 y 坐標(biāo)。
例子:
# 導(dǎo)入模塊 from bokeh.plotting import figure, output_file, show from bokeh.palettes import magma import pandas as pd # 實例化圖形對象 graph = figure(title = "Bokeh Scatter Graph") # 讀取數(shù)據(jù)庫 data = pd.read_csv("tips.csv") color = magma(256) # 繪制圖形 graph.scatter(data['total_bill'], data['tip'], color=color) # 展示模型 show(graph)
輸出:
折線圖
例子:
# 導(dǎo)入模塊from bokeh.plotting import figure, output_file, showimport pandas as pd# 實例化圖形對象graph = figure(title = "Bokeh Bar Chart")# 讀取數(shù)據(jù)庫data = pd.read_csv("tips.csv")# 提示列的每個唯一值的計數(shù)df = data['tip'].value_counts()# 繪制圖形graph.line(df, data['tip'])# 展示模型show(graph)
輸出:
條形圖
條形圖可以有水平條和垂直條兩種類型。 每個都可以分別使用繪圖界面的 hbar() 和 vbar() 函數(shù)創(chuàng)建。
例子:
# 導(dǎo)入模塊from bokeh.plotting import figure, output_file, showimport pandas as pd# 實例化圖形對象graph = figure(title = "Bokeh Bar Chart")# 讀取數(shù)據(jù)庫data = pd.read_csv("tips.csv")# 繪制圖形graph.vbar(data['total_bill'], top=data['tip'])# 展示模型show(graph)
輸出:
交互式數(shù)據(jù)可視化
Bokeh 的主要功能之一是為繪圖添加交互性。 讓我們看看可以添加的各種交互。
Interactive Legends
click_policy
屬性使圖例具有交互性。 有兩種類型的交互
- 隱藏:隱藏字形。
- 靜音:隱藏字形使其完全消失,另一方面,靜音字形只是根據(jù)參數(shù)去強調(diào)字形。
例子:
# 導(dǎo)入模塊 from bokeh.plotting import figure, output_file, show import pandas as pd # 實例化圖形對象 graph = figure(title = "Bokeh Bar Chart") # 讀取數(shù)據(jù)庫 data = pd.read_csv("tips.csv") # 繪制圖形 graph.vbar(data['total_bill'], top=data['tip'], legend_label = "Bill VS Tips", color='green') graph.vbar(data['tip'], top=data['size'], legend_label = "Tips VS Size", color='red') graph.legend.click_policy = "hide" # 展示模型 show(graph)
輸出:
添加小部件
Bokeh 提供了類似于 HTML 表單的 GUI 功能,如按鈕、滑塊、復(fù)選框等。這些為繪圖提供了一個交互界面,允許更改繪圖參數(shù)、修改繪圖數(shù)據(jù)等。讓我們看看如何使用和添加一些常用的小部件。
按鈕
這個小部件向繪圖添加了一個簡單的按鈕小部件。 我們必須將自定義 JavaScript 函數(shù)傳遞給模型類的 CustomJS() 方法。
復(fù)選框
向圖中添加標(biāo)準(zhǔn)復(fù)選框。與按鈕類似,我們必須將自定義 JavaScript 函數(shù)傳遞給模型類的 CustomJS() 方法。
單選按鈕
添加一個簡單的單選按鈕并接受自定義 JavaScript 函數(shù)。
例子:
from bokeh.io import show from bokeh.models import Button, CheckboxGroup, RadioGroup, CustomJS button = Button(label="GFG") button.js_on_click(CustomJS( code="console.log('button: click!', this.toString())")) # 復(fù)選框和單選按鈕的標(biāo)簽 L = ["First", "Second", "Third"] # 活動參數(shù)集默認(rèn)檢查選定的值 checkbox_group = CheckboxGroup(labels=L, active=[0, 2]) checkbox_group.js_on_click(CustomJS(code=""" console.log('checkbox_group: active=' + this.active, this.toString()) """)) # 活動參數(shù)集默認(rèn)檢查選定的值 radio_group = RadioGroup(labels=L, active=1) radio_group.js_on_click(CustomJS(code=""" console.log('radio_group: active=' + this.active, this.toString()) """)) show(button) show(checkbox_group) show(radio_group)
輸出:
注意: 所有這些按鈕都將在新選項卡上打開。
滑塊: 向繪圖添加一個滑塊。 它還需要一個自定義的 JavaScript 函數(shù)。
示例:
from bokeh.io import show from bokeh.models import CustomJS, Slider slider = Slider(start=1, end=20, value=1, step=2, title="Slider") slider.js_on_change("value", CustomJS(code=""" console.log('slider: value=' + this.value, this.toString()) """)) show(slider)
輸出:
同樣,更多的小部件可用,如下拉菜單或選項卡小部件可以添加。
下一節(jié)我們繼續(xù)談第四個庫—— Plotly
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
python中實現(xiàn)將多個print輸出合成一個數(shù)組
下面小編就為大家分享一篇python中實現(xiàn)將多個print輸出合成一個數(shù)組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04用Python編寫一個每天都在系統(tǒng)下新建一個文件夾的腳本
這篇文章主要介紹了用Python編寫一個每天都在系統(tǒng)下新建一個文件夾的腳本,雖然這個實現(xiàn)聽起來有點無聊...但卻是學(xué)習(xí)os和time模塊的一個小實踐,需要的朋友可以參考下2015-05-05Python入門_淺談for循環(huán)、while循環(huán)
下面小編就為大家?guī)硪黄狿ython入門_淺談for循環(huán)、while循環(huán)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05在Pytorch中自定義dataset讀取數(shù)據(jù)的實現(xiàn)代碼
這篇文章給大家介紹了如何在Pytorch中自定義dataset讀取數(shù)據(jù),文中給出了詳細(xì)的圖文介紹和代碼講解,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12Python datetime和unix時間戳之間相互轉(zhuǎn)換的講解
今天小編就為大家分享一篇關(guān)于Python datetime和unix時間戳之間相互轉(zhuǎn)換的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04