美文网首页
一、Bokeh入门

一、Bokeh入门

作者: Bllose | 来源:发表于2021-03-11 06:16 被阅读0次

通过“中级”接口 - bokeh.plotting,介绍Bokeh的可视化过程。

最基础的编写包含如下三个部分

  1. 导入
  2. 定义画布,绘画
  3. 输出

样例1:
1、导入

from bokeh.io import output_file, show
from bokeh.plotting import figure

plotting 可以理解为一堆用于作图的工具, 而figure就是其中的一页用于作图的纸
output_file 用于指定输出到的文件。 如果使用Jupyter Notebook/ Lab的话, 使用output_notebook更合适。
show 则是代码层面告诉程序, 我已经画完了, 可以输出了的指令。

2、 定义画布, 绘画。 该案例为一张“散点图”

p = figure(plot_width=400, plot_height=400)

p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, line_color="navy", fill_color="orange", fill_alpha=0.5)

通过figure(plot_width=400, plot_height=400)定义好一个400x400的一页画布, 取个名字叫p
通过定义一个“记号”(glyph) p.circle 告诉程序, 我想用圆来做标记。
circle参数

[1, 2, 3, 4, 5], [6, 7, 2, 4, 5] 横坐标,纵坐标
size=15 Bokeh的所有散列标记均接受size参数, 指定散列标记的大小, 该大小以背景空间的单个元素大小作为单位。该案例中, 特别的由于标记点为圆形, 所以也接受redius作为参数, 定义标点的半径大小

3、 输出

output_file("file_name.html")  
show(p)  
example

矩形散列图

如果想用矩形作为标记作图, 那么使用square方法即可。

# create a new plot using figure
p = figure(plot_width=400, plot_height=400)

# add a square renderer with a size, color, alpha, and sizes
p.square([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=[10, 15, 20, 25, 30], color="firebrick", alpha=0.6)

show(p) # show the results

注意到上面的例子中, 我们针对每一个独立的标记设定了大小。

example2

线条图

# create a new plot (with a title) using figure
p = figure(plot_width=400, plot_height=400, title="My Line Plot")

# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)

show(p) # show the results  

线条图案中除了line_width参数以外, 还有例如line_color或者line_dash参数可以设定。

example2

时间坐标

本案例为,葡萄糖含量随时间的变化

from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.sampledata.glucose import data
data.head()

# reduce data size to one week
week = data.loc['2010-10-01':'2010-10-08']

p = figure(x_axis_type="datetime", title="Glocose Range", plot_height=350, plot_width=800)
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'

p.line(week.index, week.glucose)

output_file("time_axes.html")
show(p)
葡萄糖随时间变化曲线图

bokeh.sampledata.glucose.data 来源于pandas下的DataFrame
week = data.loc['2010-10-01':'2010-10-08'] 用于创建一个有关时间的列表, 通过print(week.index)print(week.glucose)我们可以分别看到如下信息:

DatetimeIndex(['2010-10-01 00:04:00', '2010-10-01 00:09:00',
              '2010-10-01 00:14:00', '2010-10-01 00:19:00',
              '2010-10-01 00:24:00', '2010-10-01 00:29:00',
              '2010-10-01 00:34:00', '2010-10-01 00:39:00',
              '2010-10-01 00:44:00', '2010-10-01 00:49:00',
              ...
              '2010-10-08 23:12:00', '2010-10-08 23:17:00',
              '2010-10-08 23:22:00', '2010-10-08 23:27:00',
              '2010-10-08 23:32:00', '2010-10-08 23:37:00',
              '2010-10-08 23:42:00', '2010-10-08 23:47:00',
              '2010-10-08 23:52:00', '2010-10-08 23:57:00'],
             dtype='datetime64[ns]', name='datetime', length=2217, freq=None)  
 datetime
 2010-10-01 00:04:00     92
 2010-10-01 00:09:00    100
 2010-10-01 00:14:00    108
 2010-10-01 00:19:00    115
 2010-10-01 00:24:00    120
                       ... 
 2010-10-08 23:37:00     99
 2010-10-08 23:42:00     99
 2010-10-08 23:47:00     98
 2010-10-08 23:52:00     97
 2010-10-08 23:57:00     97
 Name: glucose, Length: 2217, dtype: int64

Pandas 是一个强大的分析结构化数据的工具集。DataFrame和Series都是其厉害的武器。
该案例中,行坐标时间由DataFrame快速生成, 列坐标通过Series快速生成
本案例中新出现其他内容:

figure(x_axis_type="datetime", 定义画布的x轴坐标类型为datatime
p.xgrid.grid_line_color=None x轴上的网格线透明, 即让网格线中的竖线消失
p.ygrid.grid_line_alpha=0.5 y轴上的网格线半透明,即让网格线中的横线以半透明形式展示

六边形图案样例

import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.palettes import Viridis256
from bokeh.util.hex import hexbin

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

bins = hexbin(x, y, 0.1)

# color map the bins by hand, will see how to use linear_cmap later
color = [Viridis256[int(i)] for i in bins.counts/max(bins.counts)*255]

# match_aspect ensures neither dimension is squished, regardless of the plot size
p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#440154')
p.grid.visible = False

p.hex_tile(bins.q, bins.r, size=0.1, line_color=None, fill_color=color)

output_file("hex_tiling.html")
show(p)

运行效果

Hex_Tiling.PNG

NumPy 是使用Python进行科学计算的基础软件包
Viridis256 是Bokeh提供的一套预设(大)调色板

Palettes.PNG

通过修改上面代码, 换一个调色板

import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.util.hex import hexbin
# from bokeh.palettes import Viridis256
from bokeh.palettes import Inferno256

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

bins = hexbin(x, y, size=0.1)

# color = [Viridis256[int(i)] for i in bins.counts/max(bins.counts)*255]
color = [Inferno256[int(i)] for i in bins.counts/max(bins.counts)*255]

# p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#440154')
p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#000003')

p.grid.visible = False

p.hex_tile(bins.q, bins.r, size=0.1, line_color=None, fill_color=color)

output_file("hex_tiling.html")
show(p)

效果如下

Hex_Tiling_1.PNG

本章节我们接触了Bokeh的基本逻辑过程, 包括:
1、导入
2、定义画布、画散列标记或曲线
3、输出

同时也感受了一下Pandas和NumPy功能的强大, 为我们学习作图的初期阶段提供了巨大的便利

下一篇 二、Bokeh风格与主题

相关文章

网友评论

      本文标题:一、Bokeh入门

      本文链接:https://www.haomeiwen.com/subject/ekdvqltx.html