Python 數(shù)據(jù)可視化之Bokeh詳解
安裝
要安裝此類型,請(qǐng)?jiān)诮K端中輸入以下命令。
pip install bokeh

散點(diǎn)圖
散點(diǎn)圖中散景可以使用繪圖模塊的散射()方法被繪制。這里分別傳遞 x 和 y 坐標(biāo)。
例子:
# 導(dǎo)入模塊
from bokeh.plotting import figure, output_file, show
from bokeh.palettes import magma
import pandas as pd
# 實(shí)例化圖形對(duì)象
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# 實(shí)例化圖形對(duì)象graph = figure(title = "Bokeh Bar Chart")# 讀取數(shù)據(jù)庫data = pd.read_csv("tips.csv")# 提示列的每個(gè)唯一值的計(jì)數(shù)df = data['tip'].value_counts()# 繪制圖形graph.line(df, data['tip'])# 展示模型show(graph)
輸出:

條形圖
條形圖可以有水平條和垂直條兩種類型。 每個(gè)都可以分別使用繪圖界面的 hbar() 和 vbar() 函數(shù)創(chuàng)建。
例子:
# 導(dǎo)入模塊from bokeh.plotting import figure, output_file, showimport pandas as pd# 實(shí)例化圖形對(duì)象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ù)去強(qiáng)調(diào)字形。
例子:
# 導(dǎo)入模塊
from bokeh.plotting import figure, output_file, show
import pandas as pd
# 實(shí)例化圖形對(duì)象
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ù)選框等。這些為繪圖提供了一個(gè)交互界面,允許更改繪圖參數(shù)、修改繪圖數(shù)據(jù)等。讓我們看看如何使用和添加一些常用的小部件。
按鈕
這個(gè)小部件向繪圖添加了一個(gè)簡(jiǎn)單的按鈕小部件。 我們必須將自定義 JavaScript 函數(shù)傳遞給模型類的 CustomJS() 方法。
復(fù)選框
向圖中添加標(biāo)準(zhǔn)復(fù)選框。與按鈕類似,我們必須將自定義 JavaScript 函數(shù)傳遞給模型類的 CustomJS() 方法。
單選按鈕
添加一個(gè)簡(jiǎn)單的單選按鈕并接受自定義 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"]
# 活動(dòng)參數(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())
"""))
# 活動(dòng)參數(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)
輸出:



注意: 所有這些按鈕都將在新選項(xiàng)卡上打開。
滑塊: 向繪圖添加一個(gè)滑塊。 它還需要一個(gè)自定義的 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)
輸出:

同樣,更多的小部件可用,如下拉菜單或選項(xiàng)卡小部件可以添加。
下一節(jié)我們繼續(xù)談第四個(gè)庫—— Plotly
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- Python中2種常用數(shù)據(jù)可視化庫Bokeh和Altair使用示例詳解
- 使用Python的數(shù)據(jù)可視化庫Matplotlib實(shí)現(xiàn)折線圖
- 探索Python數(shù)據(jù)可視化庫中Plotly Express的使用方法
- Python數(shù)據(jù)可視化庫seaborn的使用總結(jié)
- Python與數(shù)據(jù)科學(xué)工具鏈之NumPy、Pandas、Matplotlib快速上手教程
- Python使用Matplotlib繪制專業(yè)柱狀圖的完整指南
- Python安裝Matplotlib庫的五種方法小結(jié)
- Python?seaborn數(shù)據(jù)可視化繪圖(直方圖,密度圖,散點(diǎn)圖)
- Python利用Seaborn繪制多標(biāo)簽的混淆矩陣
- python使用seaborn繪圖直方圖displot,密度圖,散點(diǎn)圖
- python使用Plotly創(chuàng)建交互式數(shù)據(jù)可視化的操作步驟
- 詳解如何使用Python的Plotly庫進(jìn)行交互式圖形可視化
- Python使用Plotly繪制常見5種動(dòng)態(tài)交互式圖表
- 淺談python可視化包Bokeh
- Python數(shù)據(jù)可視化庫:Matplotlib、Seaborn、Plotly、Bokeh等對(duì)比與選擇
相關(guān)文章
python實(shí)現(xiàn)Mysql數(shù)據(jù)庫批量新增數(shù)據(jù)的場(chǎng)景分析
這篇文章主要介紹了python實(shí)現(xiàn)Mysql數(shù)據(jù)庫批量新增數(shù)據(jù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
使用Python高效實(shí)現(xiàn)PDF內(nèi)容差異對(duì)比的方法詳解
PDF是一種便攜文檔格式,便于跨操作系統(tǒng)傳播文檔,PDF文檔遵循標(biāo)準(zhǔn)格式,因此存在很多可以操作PDF文檔的工具,Python自然也不例外,本文將給大家介紹了如何使用Python高效實(shí)現(xiàn)PDF內(nèi)容差異對(duì)比,需要的朋友可以參考下2025-06-06
Python實(shí)現(xiàn)將CSV轉(zhuǎn)換為帶格式的Excel
在日常的數(shù)據(jù)處理和報(bào)表生成工作中,CSV 格式因其簡(jiǎn)潔性而被廣泛采用,本文將介紹如何使用 Python 將 CSV 文件轉(zhuǎn)換為 Excel 文件,希望對(duì)大家有所幫助2025-07-07
python環(huán)境中的概念conda中與環(huán)境相關(guān)指令操作
這篇文章主要介紹了python環(huán)境中的概念conda中與環(huán)境相關(guān)指令操作,虛擬環(huán)境是從電腦獨(dú)立開辟出來的環(huán)境,文章介紹了相關(guān)概念,需要的朋友可以參考下2023-03-03
Python 數(shù)據(jù)化運(yùn)營(yíng)之KMeans聚類分析總結(jié)
這篇文章主要介紹了Python 數(shù)據(jù)化運(yùn)營(yíng)KMeans聚類相關(guān)的一些總結(jié),感興趣的話一起來閱讀下文吧2021-08-08
python轉(zhuǎn)化excel數(shù)字日期為標(biāo)準(zhǔn)日期操作
這篇文章主要介紹了python轉(zhuǎn)化excel數(shù)字日期為標(biāo)準(zhǔn)日期操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07

