http://bokeh.pydata.org/en/latest/docs/user_guide/quickstart.html#userguide-quickstart
安装
可以使用conda或者pip安装
conda
install boken
pip
install bokeh
开始
例1 基本过程
from bokeh.plotting import figure, output_file, show
# 准本数据集
x = [
1,
2,
3,
4,
5]
y = [
6,
7,
2,
4,
5]
# 输出静态的html文件
output_file(
"lines.html")
# 创建figure并设置相关属性
p = figure(title=
"simple line example", x_axis_label=
'x', y_axis_label=
'y')
# 渲染直线图像
p.line(x, y, legend=
"Temp.", line_width=
2)
# 查看输出结果,会直接打开html
show(p)
例2 调整mode
import numpy
as np
from bokeh.plotting import figure, output_file, show
N =
4000
x = np.
random.
random(size=N) *
100
y = np.
random.
random(size=N) *
100
radii = np.
random.
random(size=N) *
1.5
colors = [
"#xxx" % (int(r), int(g),
150)
for r, g
in zip(
50+
2*x,
30+
2*y)
]
output_file(
"color_scatter.html", title=
"color_scatter.py example", mode=
"cdn")
TOOLS=
"resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"
p = figure(tools=TOOLS, x_range=(
0,
100), y_range=(
0,
100))
p.circle(x,y, radius=radii, fill_color=colors, fill_alpha=
0.6, line_color=None)
show(p)
例3 创建关联图形
linked panning (range联动)
import numpy as np
from bokeh
.layouts import gridplot
from bokeh
.plotting import figure, output_file, show
# 准备数据集
N =
100
x = np
.linspace(
0,
4*np
.pi, N)
y0 = np
.sin(
x)
y1 = np
.cos(
x)
y2 = np
.sin(
x) + np
.cos(
x)
# 输出静态html文件
output_file(
"linked_panning.html")
# 创建第一个figure对象
s1 = figure(width=
250, plot_height=
250, title=None)
s1
.circle(
x, y0, size=
10, color=
"navy", alpha=
0.5)
# NEW: 创建第二个figure对象,共享双轴
s2 = figure(width=
250, height=
250, x_range=s1
.x_range, y_range=s1
.y_range, title=None)
s2
.triangle(
x, y1, size=
10, color=
"firebrick", alpha=
0.5)
# NEW: 创建一个figure对象,共享单轴
s3 = figure(width=
250, height=
250, x_range=s1
.x_range, title=None)
s3
.square(
x, y2, size=
10, color=
"olive", alpha=
0.5)
# NEW: 将所有的figure放到一个figure中
p = gridplot([[s1, s2, s3]], toolbar_location=None)
# 展示最后的结果
show(p)
linked brushing(source联动)
import numpy
as np
from bokeh.plotting import *
from bokeh.models import ColumnDataSource
N =
300
x = np.linspace(
0,
4*np.
pi, N)
y0 = np.
sin(x)
y1 = np.
cos(x)
output_file(
"linked_brushing.html")
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))
TOOLS =
"pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select"
left = figure(tools=TOOLS, width=
350, height=
350, title=None)
left.circle(
'x',
'y0', source=source)
right = figure(tools=TOOLS, width=
350, height=
350, title=None)
right.circle(
'x',
'y1', source=source)
p = gridplot([[left,
right]])
show(p)
例4 时间序列处理
使用x_axis_type=”datetime”指定
Bokeh Plot Server