天天看點

使用 Python 進行資料可視化之Bokeh

安裝

要安裝此類型,請在終端中輸入以下指令。

pip install bokeh      
使用 Python 進行資料可視化之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")
# 讀取資料庫
data = pd.read_csv("tips.csv")
color = magma(256)
# 繪制圖形
graph.scatter(data['total_bill'], data['tip'], color=color)
# 展示模型
show(graph)      

輸出:

使用 Python 進行資料可視化之Bokeh

折線圖

# 導入子產品
from bokeh.plotting import figure, output_file, show
import pandas as pd
# 執行個體化圖形對象
graph = figure(title = "Bokeh Bar Chart")
# 讀取資料庫
data = pd.read_csv("tips.csv")
# 提示列的每個唯一值的計數
df = data['tip'].value_counts()
# 繪制圖形
graph.line(df, data['tip'])
# 展示模型
show(graph)      
使用 Python 進行資料可視化之Bokeh

條形圖

條形圖可以有水準條和垂直條兩種類型。 每個都可以分别使用繪圖界面的 hbar() 和 vbar() 函數建立。

# 導入子產品
from bokeh.plotting import figure, output_file, show
import pandas as pd
# 執行個體化圖形對象
graph = figure(title = "Bokeh Bar Chart")
# 讀取資料庫
data = pd.read_csv("tips.csv")
# 繪制圖形
graph.vbar(data['total_bill'], top=data['tip'])
# 展示模型
show(graph)      
使用 Python 進行資料可視化之Bokeh

互動式資料可視化

Bokeh 的主要功能之一是為繪圖添加互動性。 讓我們看看可以添加的各種互動。

Interactive Legends

click_policy 屬性使圖例具有互動性。 有兩種類型的互動

隐藏:隐藏字形。

靜音:隐藏字形使其完全消失,另一方面,靜音字形隻是根據參數去強調字形。

# 導入子產品
from bokeh.plotting import figure, output_file, show
import pandas as pd
# 執行個體化圖形對象
graph = figure(title = "Bokeh Bar Chart")
# 讀取資料庫
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)      
使用 Python 進行資料可視化之Bokeh

添加小部件

Bokeh 提供了類似于 HTML 表單的 GUI 功能,如按鈕、滑塊、複選框等。這些為繪圖提供了一個互動界面,允許更改繪圖參數、修改繪圖資料等。讓我們看看如何使用和添加一些常用的小部件。

按鈕

這個小部件向繪圖添加了一個簡單的按鈕小部件。 我們必須将自定義 JavaScript 函數傳遞給模型類的 CustomJS() 方法。

複選框

向圖中添加标準複選框。與按鈕類似,我們必須将自定義 JavaScript 函數傳遞給模型類的 CustomJS() 方法。

單選按鈕

添加一個簡單的單選按鈕并接受自定義 JavaScript 函數。

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"]
# 活動參數集預設檢查標明的值
checkbox_group = CheckboxGroup(labels=L, active=[0, 2])
checkbox_group.js_on_click(CustomJS(code="""
    console.log('checkbox_group: active=' + this.active, this.toString())
"""))
# 活動參數集預設檢查標明的值
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)      
使用 Python 進行資料可視化之Bokeh

注意: 所有這些按鈕都将在新頁籤上打開。

滑塊: 向繪圖添加一個滑塊。 它還需要一個自定義的 JavaScript 函數。

示例:

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)      
使用 Python 進行資料可視化之Bokeh

同樣,更多的小部件可用,如下拉菜單或頁籤小部件可以添加。