Bokeh快速入门

绘图步骤:

  • 准备数据
  • 选择结果输出方式

可以用output_file()输出为"lines.html". 也可以使用output_notebook()在 Jupyter notebooks中直接展示。

  • figure()绘制画布
  • 绘制图形,如line()
  • 显示绘图结果

举个栗子:

01_显示多条曲线,用用output_file()展示:

from bokeh.plotting import figure, output_file, show

# 准备数据
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]

# 输出为静态的html
output_file("log_lines.html")

# 创建画布
p = figure(
   tools="pan,box_zoom,reset,save",
   y_axis_type="log", y_range=[0.001, 10**11], title="log axis example",
   x_axis_label='sections', y_axis_label='particles'
)

# 添加曲线
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x", fill_color="white", size=8)
p.line(x, y0, legend="y=x^2", line_width=3)
p.line(x, y1, legend="y=10^x", line_color="red")
p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")

# 显示结果
show(p)

PS:正因为Matplotlib的图太丑,参数设置复杂;Plotly需要注册才能使用更多功能;Seaborn对高版本Python支持不是很友好(本主在2016年放弃Seaborn,现在好很多了),且同ggplot2对Flask支持不是很友好,本主当时需要实现Flask数据可视化(如下,纯Flask与Bokeh交互)。

02_向量化的颜色和大小:

03_网格显示并通过数据链接同时移动数据:

import numpy as np

from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show

# prepare some data
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)

# output to static HTML file
output_file("linked_panning.html")

# create a new plot
s1 = figure(width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)

# NEW: create a new plot and share both ranges
# 新:图2的x,y轴范围与图1链接(移动时,x,y同时移动)
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)

# 新:图3仅x轴范围与图1链接(移动时,仅x轴同时移动)
s3 = figure(width=250, height=250, x_range=s1.x_range, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)

# 新:网格显示3张图,并不显示工具栏
p = gridplot([[s1, s2, s3]], toolbar_location=None)

# show the results
show(p)

04_网格显示并通过选框同时选定特定数据:

05_时间序列显示:

import numpy as np

from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import AAPL

# prepare some data
aapl = np.array(AAPL['adj_close'])
aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)

window_size = 30
window = np.ones(window_size)/float(window_size)
aapl_avg = np.convolve(aapl, window, 'same')

# output to static HTML file
output_file("stocks.html", title="stocks.py example")

# create a new plot with a a datetime axis type
# 注:这里x_axis_type="datetime",Bokeh早期版本直接支持pandas的时间序列;还有就是时间格式对中文不友好
p = figure(plot_width=800, plot_height=350, x_axis_type="datetime")

# add renderers
p.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close')
p.line(aapl_dates, aapl_avg, color='navy', legend='avg')

# NEW: customize by setting attributes
# 新:关于画布的一些个性化定义
p.title.text = "AAPL One-Month Average"
p.legend.location = "top_left"
p.grid.grid_line_alpha = 0
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.ygrid.band_fill_color = "olive"
p.ygrid.band_fill_alpha = 0.1

# show the results
# 显示结果
show(p)

未经允许不得转载!Bokeh快速入门