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爬取YY評級分?jǐn)?shù)并保存數(shù)據(jù)實現(xiàn)過程解析
這篇文章主要介紹了Python爬取YY評級分?jǐn)?shù)并保存數(shù)據(jù)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python Pandas實現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例
今天小編就為大家分享一篇Python Pandas實現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python深度學(xué)習(xí)pytorch神經(jīng)網(wǎng)絡(luò)塊的網(wǎng)絡(luò)之VGG
雖然AlexNet證明深層神經(jīng)網(wǎng)絡(luò)卓有成效,但它沒有提供一個通用的模板來指導(dǎo)后續(xù)的研究人員設(shè)計新的網(wǎng)絡(luò)。下面,我們將介紹一些常用于設(shè)計深層神經(jīng)網(wǎng)絡(luò)的啟發(fā)式概念2021-10-10
Python批量將Word文檔(.doc)轉(zhuǎn)換為.docx格式的完整實現(xiàn)步驟
這篇文章主要介紹了Python批量將Word文檔(.doc)轉(zhuǎn)換為.docx格式的完整實現(xiàn)步驟,文中通過代碼介紹的非常詳細(xì),適用于Windows系統(tǒng),解決了手動轉(zhuǎn)換的低效率和出錯率問題,需要的朋友可以參考下2024-12-12
Biblibili視頻投稿接口分析并以Python實現(xiàn)自動投稿功能
這篇文章主要介紹了Biblibili視頻投稿接口分析并以Python實現(xiàn)自動投稿功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

