Bokeh 系列文章传送门:

  • Bokeh小册子:入门

  • Bokeh小册子:figure详细解读

  • Bokeh基础可视化图形

  • Bokeh中独特的数据类型简介: ColumnDataSource | Bokeh 小册子


本文主要介绍 在 bokeh 中 对 ColumnDataSource 类型数据中进行:

(1)添加新的数据 (2)修改更新已有数据 (3)筛选数据。


Table of Contents

  • 1  添加新的数据

  • 2  数据更新

    • 2.1  更新单个数据

    • 2.2  更新多个数据

  • 3  筛选数据

    • 3.1  Indexfilter

    • 3.2  BooleanFilter

    • 3.3  GroupFilter

本文的环境为

  • window 7 系统

  • python 3.6

  • Jupyter Notebook

  • bokeh 0.13.0

数据是进行数据可视化的必要基础, 前文介绍了 bokeh 中数据呈现的几种方式。

本文主要介绍 在 bokeh 中 对 ColumnDataSource 类型数据中进行:

(1)添加新的数据 (2)修改更新已有数据 (3)筛选数据。

首先加载相关Python库。

from bokeh.plotting import figure, output_notebook, showfrom bokeh.layouts import gridplotfrom bokeh.models import ColumnDataSourceimport numpy as npimport pandas as pdoutput_notebook()

1 添加新的数据

ColumnDataSource 通过 stream() 方法来添加新的数据

data1 = {'x_values': [1, 2, 9, 4, 5],        'y_values': [6, 7, 2, 3, 6]}source = ColumnDataSource(data=data1)p1 = figure(plot_width=300, plot_height=300, title= 'origin data')p1.circle(x='x_values', y='y_values', source=source, size=20)show(p1)

图示如下:

new_data = {'x_values':  [6, 7, 2, 3, 6],        'y_values': [1, 2, 9, 4, 5]}# 在已有数据基础上添加新的数据 (append)source.stream(new_data)p2 = figure(plot_width=300, plot_height=300,title= 'append data with stream')p2.circle(x='x_values', y='y_values', source=source, size=20)show(p2)

图示如下:

2 数据更新

用 patch 方法可以更新 ColumnDataSource 的数据。

data = {'x_column': [1, 2, 9, 4, 5, 8],        'y_column': [6, 7, 2, 3, 6, 2]}df = pd.DataFrame(data=data)df

如下:

# 更新单个数据data = {'x_column': [1, 2, 9, 4, 5, 8],        'y_column': [6, 7, 2, 3, 6, 2]}df = pd.DataFrame(data=data)df

图示如下:

2.1 更新单个数据

# {column:(index, new_value) }source.patch({'x_column':[(0,15)]})p2 = figure(plot_width=300, plot_height=300,title= 'revise single value with patch')p2.circle(x='x_column', y='y_column', source=source, size=20)show(p2)

图示如下:

2.2 更新多个数据

# 更新多个数据# {column:(slice, new_values) }s = slice(2,4)source.patch({'x_column':[(s,[20,15])]})p2 = figure(plot_width=300, plot_height=300,title= 'revise multiple values with patch')p2.circle(x='x_column', y='y_column', source=source, size=20)show(p2)

图示如下:

3 筛选数据

在Bokeh 中, ColumnDataSource (CDS) 提供了 CDSView 来对数据进行筛选

其一般用法如下:

from bokeh.models import ColumnDataSource, CDSView

source = ColumnDataSource(some_data)

view = CDSView(source=source, filters=[filter1, filter2])

其中 filters 包括 IndexFilter, BooleanFilter, GroupFilter 等

3.1 Indexfilter

根据数据的索引来筛选数据

from bokeh.plotting import figurefrom bokeh.models import ColumnDataSource, CDSView, IndexFilterfrom bokeh.layouts import gridplotdata = {'x_column': [1, 2, 9, 4, 5, 8],        'y_column': [6, 7, 2, 3, 6, 2]}df = pd.DataFrame(data=data)source = ColumnDataSource(data=df)view = CDSView(source=source, filters=[IndexFilter([0,2,4])])p1 = figure(plot_width=300, plot_height=300, title='origin state')p1.circle(x='x_column', y='y_column', source=source, size=20)p2 = figure(plot_width=300, plot_height=300, title='IndexFilter')p2.circle(x='x_column', y='y_column', source=source, size=20, view=view)grid=gridplot([p1,p2],ncols=2, plot_width=300,plot_height=300)show(grid)

图示如下:

3.2 BooleanFilter

根据布尔值, True 或 False 来筛选数据

from bokeh.models import BooleanFilterfrom bokeh.layouts import rowbooleans = [True if y_val>4 else False for y_val in source.data['y_column']]view_booleans = CDSView(source=source, filters=[BooleanFilter(booleans)])p1 = figure(plot_width=300, plot_height=300,title='origin state')p1.circle(x='x_column', y='y_column', source=source, size=20)p2 = figure(plot_width=300, plot_height=300, title='BooleanFilter')p2.circle(x='x_column', y='y_column', source=source, size=20, view=view_booleans)grid=gridplot([p1,p2],ncols=2,plot_width=300,plot_height=300)show(grid)

图示如下:

3.3 GroupFilter

使用 GroupFilter 可以筛选出包含特定类型的所有行数据。 GroupFilter 有两个参数, 即 Column_name 和 group, 也就是 列名 和 类别名称。

如下面的官方例子所述,如果想筛选 iris 数据中 特定类别的花,可以使用 GroupFilter 方法。

from bokeh.plotting import figure, showfrom bokeh.layouts import gridplotfrom bokeh.models import ColumnDataSource, CDSView, GroupFilterfrom bokeh.sampledata.iris import flowers# output_file("group_filter.html")source = ColumnDataSource(flowers)view1 = CDSView(source=source, filters=[GroupFilter(column_name='species', group='versicolor')])plot_size_and_tools = {'plot_height': 300, 'plot_width': 300,from bokeh.plotting import figure, showfrom bokeh.layouts import gridplotfrom bokeh.models import ColumnDataSource, CDSView, GroupFilterfrom bokeh.sampledata.iris import flowers# output_file("group_filter.html")source = ColumnDataSource(flowers)view1 = CDSView(source=source, filters=[GroupFilter(column_name='species', group='versicolor')])plot_size_and_tools = {'plot_height': 300, 'plot_width': 300,                        'tools':['box_select', 'reset', 'help']}p1 = figure(title="Full data set", **plot_size_and_tools)p1.circle(x='petal_length', y='petal_width', source=source, color='black')p2 = figure(title="Setosa only", x_range=p1.x_range, y_range=p1.y_range, **plot_size_and_tools)p2.circle(x='petal_length', y='petal_width', source=source, view=view1, color='red')show(gridplot([[p1, p2]]))

图示如下:

当然,还有一些其他的筛选方法,有兴趣的同学可以自己挖掘下~~


---------------- End ----------------


点击前往【项目实战】

世界杯系列 | 福布斯系列 | 求职系列


知识星球

我的知识星球【Python数据之道成长圈】已开通,想加入的同学,请回复数字 “2” 了解详情。


果您对我的文章感兴趣或者觉得文章内容不错的话,请在阅读后顺便转发到您的圈子里,或者点个赞鼓励我继续前行! 感谢您的陪伴与支持!


如需加入微信群交流,请添加微信小助手(微信号:147121977,请备注“python”),后续将邀请入群。


©著作权归作者所有:来自51CTO博客作者mb5fe18e7c44408的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 毫秒时间戳标识消息导致数据丢失的问题排查
  2. Pandas小册子:日期数据处理 - 如何按日期筛选、显示及统计数据
  3. 2017年文章汇总 - Python数据之道
  4. Python数据类型-List介绍(下)-列表推导式
  5. Python数据类型-List介绍(上)
  6. 内卷?猝死?企业如何利用数据分析提升人效比,让员工远离“996”?
  7. 【工具】历史文章分类汇总-V6 | Python数据之道
  8. js中基础数据结构数组去重问题
  9. mysql group_concat 获取一对多的数据

随机推荐

  1. go get命令详解
  2. go语言结构体详解
  3. go语言中go build和go install的区别
  4. Go语言两种版本的Hello world你会吗
  5. go语言中接口的使用
  6. GoLang中协程图文详解
  7. 用Go语言编写一个简单的WebSocket推送服
  8. golang中gc实现原理介绍
  9. go语言的异常处理介绍
  10. golang操作Redis&Mysql&RabbitMQ的方法介