1. bokeh 简介
bokeh 是一个专注于 web 端交互式数据可视化的 python 库。它基于 javascript 的 bokehjs 进行渲染,使得生成的图表可以直接嵌入 html,并支持交互操作。与 matplotlib、seaborn 等传统静态绘图库相比,bokeh 在处理大规模数据和交互性方面具有明显优势。
1.1 为什么选择 bokeh
- 交互性强:支持缩放、平移、悬停提示等交互功能。
- 高效渲染:利用 webgl 提高大规模数据集的绘图性能。
- 与 pandas 兼容:可以直接处理 dataframe 数据。
- 易于嵌入:可将可视化结果嵌入 html、flask、django 和 jupyter notebook。
1.2 安装与环境配置
安装 bokeh 非常简单,可以通过 pip 直接安装:
pip install bokeh
安装后,可以在 python 环境中测试:
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
:绘图区域,用于创建图表。glyph
:可视化图元,如线、点、柱状图等。columndatasource
:数据源,便于管理数据和交互。output_file/output_notebook
:指定输出方式。show/save
:显示或保存图表。
2.1 创建基本绘图
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
等工具实现。
3.1 鼠标悬停显示数据
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. 数据流处理
bokeh 支持动态数据更新,适用于实时数据可视化,如传感器数据、股票市场数据等。
4.1 动态数据更新
from bokeh.models import columndatasource from bokeh.plotting import figure, curdoc import numpy as np source = columndatasource(data=dict(x=[], y=[])) p = figure(title="动态数据流", 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 服务器会持续更新数据,并在浏览器中实时展示曲线变化。
5. bokeh 与 pandas、flask/django 集成
bokeh 可以与 pandas 结合处理数据,并与 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 数据绘图") 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. 总结
bokeh 是 python 生态中最强大的交互式可视化工具之一,适用于大规模数据、web 嵌入和动态数据流可视化。它的灵活性、易用性和强大的交互能力,使其成为数据科学、金融分析、物联网数据可视化的理想选择。
到此这篇关于python 交互式可视化的利器bokeh的使用的文章就介绍到这了,更多相关python bokeh内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论