Python 交互式可視化的利器Bokeh的使用
1. Bokeh 簡介
Bokeh 是一個專注于 Web 端交互式數(shù)據(jù)可視化的 Python 庫。它基于 JavaScript 的 BokehJS 進行渲染,使得生成的圖表可以直接嵌入 HTML,并支持交互操作。與 Matplotlib、Seaborn 等傳統(tǒng)靜態(tài)繪圖庫相比,Bokeh 在處理大規(guī)模數(shù)據(jù)和交互性方面具有明顯優(yōu)勢。
1.1 為什么選擇 Bokeh
- 交互性強:支持縮放、平移、懸停提示等交互功能。
- 高效渲染:利用 WebGL 提高大規(guī)模數(shù)據(jù)集的繪圖性能。
- 與 Pandas 兼容:可以直接處理 DataFrame 數(shù)據(jù)。
- 易于嵌入:可將可視化結(jié)果嵌入 HTML、Flask、Django 和 Jupyter Notebook。
1.2 安裝與環(huán)境配置
安裝 Bokeh 非常簡單,可以通過 pip 直接安裝:
pip install bokeh
安裝后,可以在 Python 環(huán)境中測試:
from bokeh.plotting import figure, show from bokeh.io import output_file output_file("test.html") # 生成 HTML 文件 p = figure(title="示例圖", x_axis_label="X 軸", y_axis_label="Y 軸") p.line([1, 2, 3, 4], [10, 20, 30, 40], line_width=2) show(p) # 在瀏覽器中顯示圖表
運行代碼后,會在默認瀏覽器中打開一個 HTML 頁面,顯示簡單的折線圖。
2. Bokeh 基礎
Bokeh 的核心概念主要包括:
figure
:繪圖區(qū)域,用于創(chuàng)建圖表。glyph
:可視化圖元,如線、點、柱狀圖等。ColumnDataSource
:數(shù)據(jù)源,便于管理數(shù)據(jù)和交互。output_file/output_notebook
:指定輸出方式。show/save
:顯示或保存圖表。
2.1 創(chuàng)建基本繪圖
Bokeh 提供了多種基礎圖表類型,包括折線圖、散點圖、條形圖等。以下是一些常見示例。
2.1.1 折線圖
from bokeh.plotting import figure, show p = figure(title="折線圖示例", x_axis_label="X", y_axis_label="Y") p.line([1, 2, 3, 4, 5], [5, 7, 2, 3, 6], line_width=2, color="blue") show(p)
2.1.2 散點圖
p = figure(title="散點圖示例", x_axis_label="X", y_axis_label="Y") p.circle([1, 2, 3, 4, 5], [5, 7, 2, 3, 6], size=10, color="red", alpha=0.5) show(p)
2.1.3 柱狀圖
from bokeh.io import show from bokeh.plotting import figure from bokeh.transform import factor_cmap from bokeh.models import ColumnDataSource fruits = ["蘋果", "香蕉", "橙子", "葡萄"] values = [10, 20, 15, 30] source = ColumnDataSource(data=dict(fruits=fruits, values=values)) p = figure(x_range=fruits, title="水果銷量", toolbar_location=None, tools="") p.vbar(x="fruits", top="values", width=0.4, source=source) show(p)
3. 交互式功能
Bokeh 的一大亮點是交互式可視化,主要通過 HoverTool
、TapTool
、BoxSelectTool
等工具實現(xiàn)。
3.1 鼠標懸停顯示數(shù)據(jù)
from bokeh.models import HoverTool p = figure(title="懸停提示示例", x_axis_label="X", y_axis_label="Y") p.circle([1, 2, 3, 4], [10, 20, 30, 40], size=10, color="navy", alpha=0.5) hover = HoverTool(tooltips=[("X 軸", "$x"), ("Y 軸", "$y")]) p.add_tools(hover) show(p)
3.2 選擇和縮放
p = figure(title="選擇和縮放示例", tools="box_select,pan,wheel_zoom,reset") p.circle([1, 2, 3, 4], [10, 20, 30, 40], size=10, color="green", alpha=0.5) show(p)
4. 數(shù)據(jù)流處理
Bokeh 支持動態(tài)數(shù)據(jù)更新,適用于實時數(shù)據(jù)可視化,如傳感器數(shù)據(jù)、股票市場數(shù)據(jù)等。
4.1 動態(tài)數(shù)據(jù)更新
from bokeh.models import ColumnDataSource from bokeh.plotting import figure, curdoc import numpy as np source = ColumnDataSource(data=dict(x=[], y=[])) p = figure(title="動態(tài)數(shù)據(jù)流", x_axis_label="X", y_axis_label="Y") p.line("x", "y", source=source, line_width=2) def update(): new_data = dict(x=[np.random.random()], y=[np.random.random()]) source.stream(new_data, rollover=50) curdoc().add_root(p) curdoc().add_periodic_callback(update, 1000) # 每秒更新一次
運行該代碼時,Bokeh 服務器會持續(xù)更新數(shù)據(jù),并在瀏覽器中實時展示曲線變化。
5. Bokeh 與 Pandas、Flask/Django 集成
Bokeh 可以與 Pandas 結(jié)合處理數(shù)據(jù),并與 Flask 或 Django 進行 Web 應用集成。
5.1 Bokeh + Pandas
import pandas as pd data = pd.DataFrame({"x": [1, 2, 3, 4], "y": [10, 20, 30, 40]}) source = ColumnDataSource(data) p = figure(title="Pandas 數(shù)據(jù)繪圖") p.line("x", "y", source=source, line_width=2) show(p)
5.2 Bokeh + Flask
from flask import Flask, render_template from bokeh.embed import components app = Flask(__name__) @app.route("/") def index(): p = figure(title="Flask 集成示例") p.line([1, 2, 3, 4], [10, 20, 30, 40]) script, div = components(p) return render_template("index.html", script=script, div=div) if __name__ == "__main__": app.run(debug=True)
6. 總結(jié)
Bokeh 是 Python 生態(tài)中最強大的交互式可視化工具之一,適用于大規(guī)模數(shù)據(jù)、Web 嵌入和動態(tài)數(shù)據(jù)流可視化。它的靈活性、易用性和強大的交互能力,使其成為數(shù)據(jù)科學、金融分析、物聯(lián)網(wǎng)數(shù)據(jù)可視化的理想選擇。
到此這篇關(guān)于Python 交互式可視化的利器Bokeh的使用的文章就介紹到這了,更多相關(guān)Python Bokeh內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
時間序列預測中的數(shù)據(jù)滑窗操作實例(python實現(xiàn))
滑動窗口操作非常普遍,非常有用,它們也很容易在Python中實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于時間序列預測中的數(shù)據(jù)滑窗操作python實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-03-03Python3 socket即時通訊腳本實現(xiàn)代碼實例(threading多線程)
這篇文章主要介紹了Python3 socket即時通訊腳本實現(xiàn)代碼實例(threading多線程),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06python框架flask入門之環(huán)境搭建及開啟調(diào)試
這篇文章主要介紹了python框架flask入門環(huán)境搭建及開啟調(diào)試的步驟設置,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06