Python 數(shù)據(jù)可視化之Bokeh詳解
安裝
要安裝此類型,請在終端中輸入以下命令。
pip install bokeh
散點圖
散點圖中散景可以使用繪圖模塊的散射()方法被繪制。這里分別傳遞 x 和 y 坐標。
例子:
# 導入模塊 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)
輸出:
折線圖
例子:
# 導入模塊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)建。
例子:
# 導入模塊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)字形。
例子:
# 導入模塊 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 功能,如按鈕、滑塊、復選框等。這些為繪圖提供了一個交互界面,允許更改繪圖參數(shù)、修改繪圖數(shù)據(jù)等。讓我們看看如何使用和添加一些常用的小部件。
按鈕
這個小部件向繪圖添加了一個簡單的按鈕小部件。 我們必須將自定義 JavaScript 函數(shù)傳遞給模型類的 CustomJS() 方法。
復選框
向圖中添加標準復選框。與按鈕類似,我們必須將自定義 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())")) # 復選框和單選按鈕的標簽 L = ["First", "Second", "Third"] # 活動參數(shù)集默認檢查選定的值 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ù)集默認檢查選定的值 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)容!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。