欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 數(shù)據(jù)可視化之Bokeh詳解

 更新時間:2021年11月02日 10:40:05   作者:海擁✘  
這篇文章主要介紹了Python數(shù)據(jù)可視化庫Bokeh的使用總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

安裝

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

pip install bokeh

image.png

散點圖

散點圖中散景可以使用繪圖模塊的散射()方法被繪制。這里分別傳遞 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)

輸出:

image.png

折線圖

例子:

# 導(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)

輸出:

image.png

條形圖

條形圖可以有水平條和垂直條兩種類型。 每個都可以分別使用繪圖界面的 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)

輸出:

image.png

交互式數(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)

輸出:

interactivelegendsbokeh.gif

添加小部件

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)

輸出:

image.png

image.png

image.png

注意: 所有這些按鈕都將在新選項卡上打開。

滑塊: 向繪圖添加一個滑塊。 它還需要一個自定義的 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)

輸出:

bokehtutorialslider.gif

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

下一節(jié)我們繼續(xù)談第四個庫—— Plotly

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評論