Bokeh库快速入门

来源:互联网 发布:淘宝如何给差评 编辑:程序博客网 时间:2024/06/07 00:50

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)# 查看输出结果,会直接打开htmlshow(p)

例2 调整mode

import numpy as npfrom bokeh.plotting import figure, output_file, show# 准备数据N = 4000x = np.random.random(size=N) * 100y = np.random.random(size=N) * 100radii = np.random.random(size=N) * 1.5colors = [    "#%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 npfrom bokeh.layouts import gridplotfrom bokeh.plotting import figure, output_file, show# 准备数据集N = 100x = 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 npfrom bokeh.plotting import *from bokeh.models import ColumnDataSource# 准备数据N = 300x = 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

原创粉丝点击