使用WebGL加速

Bokeh对使用WebGL进行渲染提供了有限的支持。这在可视化较大的数据集时非常有用。

什么是WebGL?

WebGL 是一个JavaScript API,允许使用图形处理单元(GPU)的硬件加速在浏览器中呈现内容。WebGL是标准化的,可用于所有现代浏览器。

如何启用WebGL

要在Bokeh中启用WebGL,请设置绘图的 output_backend 属性到 "webgl"

p = figure(output_backend="webgl")

支持

只有Bokeh对象的一个子集能够在WebGL中进行渲染。一些菱形和十字形的U形图可以安全地绘制成正方形和十字形。

实例

下面是一个在启用WebGL的情况下绘制一万个散点圆的示例。请注意,即使没有任何详细程度的下采样,也可以平滑地平移和缩放该图。

import numpy as np

from bokeh.plotting import figure, output_file, show

output_file("scatter10k.html", title="scatter 10k points")

N = 10000

x = np.random.normal(0, np.pi, N)
y = np.sin(x) + np.random.normal(0, 0.2, N)

TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select"

p = figure(tools=TOOLS, output_backend="webgl")
p.circle(x, y, alpha=0.1, nonselection_alpha=0.001)

show(p)

同时,下面的图演示了用一万个点绘制一条单线。

import numpy as np

from bokeh.plotting import figure, output_file, show

output_file("line10k.html", title="line10k.py example")

N = 10000
x = np.linspace(0, 10*np.pi, N)
y = np.cos(x) + np.sin(2*x+1.25) + np.random.normal(0, 0.001, (N, ))

p = figure(title="A line consisting of 10k points", output_backend="webgl")
p.line(x, y, color="#22aa22", line_width=3)

show(p)