Python 交互式可視化的利器Bokeh的使用
1. Bokeh 簡介
Bokeh 是一個(gè)專注于 Web 端交互式數(shù)據(jù)可視化的 Python 庫。它基于 JavaScript 的 BokehJS 進(jìn)行渲染,使得生成的圖表可以直接嵌入 HTML,并支持交互操作。與 Matplotlib、Seaborn 等傳統(tǒng)靜態(tài)繪圖庫相比,Bokeh 在處理大規(guī)模數(shù)據(jù)和交互性方面具有明顯優(yōu)勢。
1.1 為什么選擇 Bokeh
- 交互性強(qiáng):支持縮放、平移、懸停提示等交互功能。
- 高效渲染:利用 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) # 在瀏覽器中顯示圖表
運(yùn)行代碼后,會(huì)在默認(rèn)瀏覽器中打開一個(gè) HTML 頁面,顯示簡單的折線圖。
2. Bokeh 基礎(chǔ)
Bokeh 的核心概念主要包括:
figure
:繪圖區(qū)域,用于創(chuàng)建圖表。glyph
:可視化圖元,如線、點(diǎn)、柱狀圖等。ColumnDataSource
:數(shù)據(jù)源,便于管理數(shù)據(jù)和交互。output_file/output_notebook
:指定輸出方式。show/save
:顯示或保存圖表。
2.1 創(chuàng)建基本繪圖
Bokeh 提供了多種基礎(chǔ)圖表類型,包括折線圖、散點(diǎn)圖、條形圖等。以下是一些常見示例。
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 散點(diǎn)圖
p = figure(title="散點(diǎn)圖示例", 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 的一大亮點(diǎn)是交互式可視化,主要通過 HoverTool
、TapTool
、BoxSelectTool
等工具實(shí)現(xiàn)。
3.1 鼠標(biāo)懸停顯示數(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 支持動(dòng)態(tài)數(shù)據(jù)更新,適用于實(shí)時(shí)數(shù)據(jù)可視化,如傳感器數(shù)據(jù)、股票市場數(shù)據(jù)等。
4.1 動(dòng)態(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="動(dòng)態(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) # 每秒更新一次
運(yùn)行該代碼時(shí),Bokeh 服務(wù)器會(huì)持續(xù)更新數(shù)據(jù),并在瀏覽器中實(shí)時(shí)展示曲線變化。
5. Bokeh 與 Pandas、Flask/Django 集成
Bokeh 可以與 Pandas 結(jié)合處理數(shù)據(jù),并與 Flask 或 Django 進(jìn)行 Web 應(yīng)用集成。
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)中最強(qiáng)大的交互式可視化工具之一,適用于大規(guī)模數(shù)據(jù)、Web 嵌入和動(dòng)態(tài)數(shù)據(jù)流可視化。它的靈活性、易用性和強(qiáng)大的交互能力,使其成為數(shù)據(jù)科學(xué)、金融分析、物聯(lián)網(wǎng)數(shù)據(jù)可視化的理想選擇。
到此這篇關(guān)于Python 交互式可視化的利器Bokeh的使用的文章就介紹到這了,更多相關(guān)Python Bokeh內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Bokeh:Python交互式可視化的利器詳解
- Python?Bokeh實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)可視化
- python使用Bokeh庫實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)的可視化
- Python使用Bokeh庫實(shí)現(xiàn)炫目的交互可視化
- Python使用Bokeh進(jìn)行交互式數(shù)據(jù)可視化
- Python使用Bokeh實(shí)現(xiàn)交互式圖表的創(chuàng)建
- Python利用Bokeh進(jìn)行數(shù)據(jù)可視化的教程分享
- Python庫?Bokeh?數(shù)據(jù)可視化實(shí)用指南
- python基于Bokeh庫制作子彈圖及瀑布圖示例教程
相關(guān)文章
python進(jìn)程類subprocess的一些操作方法例子
這篇文章主要介紹了python進(jìn)程類subprocess的一些操作方法例子,本文講解了Popen、wait、poll、kill、communicate等方法的實(shí)際操作例子,需要的朋友可以參考下2014-11-11實(shí)時(shí)獲取Python的print輸出流方法
今天小編就為大家分享一篇實(shí)時(shí)獲取Python的print輸出流方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01時(shí)間序列預(yù)測中的數(shù)據(jù)滑窗操作實(shí)例(python實(shí)現(xiàn))
滑動(dòng)窗口操作非常普遍,非常有用,它們也很容易在Python中實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于時(shí)間序列預(yù)測中的數(shù)據(jù)滑窗操作python實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-03-03Python3 socket即時(shí)通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程)
這篇文章主要介紹了Python3 socket即時(shí)通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python框架flask入門之環(huán)境搭建及開啟調(diào)試
這篇文章主要介紹了python框架flask入門環(huán)境搭建及開啟調(diào)試的步驟設(shè)置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06