Bokeh库快速入门

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 = [
    "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)
]
# 输出静态html文件爱呢(with CDN resources)
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)
# 创建静态html对象
output_file("linked_brushing.html")
# NEW: 创建一列共享数据列
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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值