当前位置: 首页 > news >正文

Bokeh中数据的添加、修改和筛选 | Bokeh 小册子

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库。

 
 
  1. from bokeh.plotting import figure, output_notebook, show

  2. from bokeh.layouts import gridplot

  3. from bokeh.models import ColumnDataSource

  4. import numpy as np

  5. import pandas as pd


  6. output_notebook()

1 添加新的数据

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

 
 
  1. data1 = {'x_values': [1, 2, 9, 4, 5],

  2. 'y_values': [6, 7, 2, 3, 6]}


  3. source = ColumnDataSource(data=data1)


  4. p1 = figure(plot_width=300, plot_height=300, title= 'origin data')

  5. p1.circle(x='x_values', y='y_values', source=source, size=20)


  6. show(p1)

图示如下:

9515a1e886ac4ac47b1e8f15ea486263feb5dc27
 
 
  1. new_data = {'x_values': [6, 7, 2, 3, 6],

  2. 'y_values': [1, 2, 9, 4, 5]}


  3. # 在已有数据基础上添加新的数据 (append)

  4. source.stream(new_data)


  5. p2 = figure(plot_width=300, plot_height=300,title= 'append data with stream')

  6. p2.circle(x='x_values', y='y_values', source=source, size=20)


  7. show(p2)

图示如下:

5999a78b3bc89b0cdcc046fbfd763142e03c62e5

2 数据更新

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

 
 
  1. data = {'x_column': [1, 2, 9, 4, 5, 8],

  2. 'y_column': [6, 7, 2, 3, 6, 2]}


  3. df = pd.DataFrame(data=data)


  4. df

如下:

97a85f6d86128c0e38a70f6ff7b25812662141bd
 
 
  1. source = ColumnDataSource(data=df)


  2. p1 = figure(plot_width=300, plot_height=300, title= 'origin data')

  3. p1.circle(x='x_column', y='y_column', source=source, size=20)


  4. show(p1)

图示如下:

6686f3b7575287ae4b7d8817695d3ed65a878242

2.1 更新单个数据

 
 
  1. # 更新单个数据

  2. # {column:(index, new_value) }

  3. source.patch({'x_column':[(0,15)]})


  4. p2 = figure(plot_width=300, plot_height=300,title= 'revise single value with patch')

  5. p2.circle(x='x_column', y='y_column', source=source, size=20)


  6. show(p2)

图示如下:

16f16a33b06343331da0a0c6c7ab4d9620d3b62a

2.2 更新多个数据

 
 
  1. # 更新多个数据

  2. # {column:(slice, new_values) }


  3. s = slice(2,4)

  4. source.patch({'x_column':[(s,[20,15])]})


  5. p2 = figure(plot_width=300, plot_height=300,title= 'revise multiple values with patch')

  6. p2.circle(x='x_column', y='y_column', source=source, size=20)


  7. show(p2)

图示如下:

486aebdd0779f2bbc070cce8cc7c178aa6d2509d

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

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

 
 
  1. from bokeh.plotting import figure

  2. from bokeh.models import ColumnDataSource, CDSView, IndexFilter

  3. from bokeh.layouts import gridplot


  4. data = {'x_column': [1, 2, 9, 4, 5, 8],

  5. 'y_column': [6, 7, 2, 3, 6, 2]}


  6. df = pd.DataFrame(data=data)

  7. source = ColumnDataSource(data=df)

  8. view = CDSView(source=source, filters=[IndexFilter([0,2,4])])


  9. p1 = figure(plot_width=300, plot_height=300, title='origin state')

  10. p1.circle(x='x_column', y='y_column', source=source, size=20)


  11. p2 = figure(plot_width=300, plot_height=300, title='IndexFilter')

  12. p2.circle(x='x_column', y='y_column', source=source, size=20, view=view)


  13. grid=gridplot([p1,p2],ncols=2, plot_width=300,plot_height=300)


  14. show(grid)

图示如下:

b05bace513133664fb36c55214a739e8e6da9748

3.2 BooleanFilter

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

 
 
  1. from bokeh.models import BooleanFilter

  2. from bokeh.layouts import row


  3. booleans = [True if y_val>4 else False for y_val in source.data['y_column']]

  4. view_booleans = CDSView(source=source, filters=[BooleanFilter(booleans)])


  5. p1 = figure(plot_width=300, plot_height=300,title='origin state')

  6. p1.circle(x='x_column', y='y_column', source=source, size=20)


  7. p2 = figure(plot_width=300, plot_height=300, title='BooleanFilter')

  8. p2.circle(x='x_column', y='y_column', source=source, size=20, view=view_booleans)


  9. grid=gridplot([p1,p2],ncols=2,plot_width=300,plot_height=300)


  10. show(grid)

图示如下:

894136407f927a58b11aeebc55af6867ea5a8a8b

3.3 GroupFilter

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

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

 
 
  1. from bokeh.plotting import figure, show

  2. from bokeh.layouts import gridplot

  3. from bokeh.models import ColumnDataSource, CDSView, GroupFilter


  4. from bokeh.sampledata.iris import flowers


  5. # output_file("group_filter.html")


  6. source = ColumnDataSource(flowers)

  7. view1 = CDSView(source=source, filters=[GroupFilter(column_name='species', group='versicolor')])


  8. plot_size_and_tools = {'plot_height': 300, 'plot_width': 300,

  9. 'tools':['box_select', 'reset', 'help']}


  10. p1 = figure(title="Full data set", **plot_size_and_tools)

  11. p1.circle(x='petal_length', y='petal_width', source=source, color='black')


  12. p2 = figure(title="Setosa only", x_range=p1.x_range, y_range=p1.y_range, **plot_size_and_tools)

  13. p2.circle(x='petal_length', y='petal_width', source=source, view=view1, color='red')


  14. show(gridplot([[p1, p2]]))

图示如下:

d9fb02b97aba02204d6e217391adc2a31162d7fb

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


原文发布时间为:2018-10-11
本文作者: Python数据之道
本文来自云栖社区合作伙伴“ ”,了解相关信息可以关注“ ”。


相关文章:

  • 2018-2019-1 20165226 《信息安全系统设计基础》第3周学习总结
  • Spring boot初体验
  • Angular2入门教程-2 实现TodoList App
  • 3、桶排序
  • Oracle DB 优化-AWR及相关内容
  • 大话 JavaScript 动画
  • Pycharm的使用一
  • iOS__上传应用到AppStore出现Authenticating with the iTunes store
  • 数字联盟刘晶晶:四年只做一个产品
  • java 通过Unsafe不使用构造器直接创建对象
  • jenkins配置用户角色权限,根据不同权限显示视图、Job
  • JVM虚拟机(五):JDK8内存模型—消失的PermGen
  • Java8 new Time Api
  • Ansible常用模块详解
  • 学以致用二十三-----shell脚本里调用脚本
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • #Java异常处理
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • Android交互
  • Angular 响应式表单 基础例子
  • JS基础篇--通过JS生成由字母与数字组合的随机字符串
  • Material Design
  • springboot_database项目介绍
  • supervisor 永不挂掉的进程 安装以及使用
  • Twitter赢在开放,三年创造奇迹
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 关于extract.autodesk.io的一些说明
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 使用agvtool更改app version/build
  • 我的面试准备过程--容器(更新中)
  • 详解NodeJs流之一
  • 一加3T解锁OEM、刷入TWRP、第三方ROM以及ROOT
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • # 数论-逆元
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (06)金属布线——为半导体注入生命的连接
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (12)目标检测_SSD基于pytorch搭建代码
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (java)关于Thread的挂起和恢复
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (二)构建dubbo分布式平台-平台功能导图
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (三分钟)速览传统边缘检测算子
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (原)Matlab的svmtrain和svmclassify
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET DevOps 接入指南 | 1. GitLab 安装
  • .net 发送邮件
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .NET项目中存在多个web.config文件时的加载顺序