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

简介:
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数据之道
本文来自云栖社区合作伙伴“ ”,了解相关信息可以关注“ ”。


相关文章
|
3月前
|
数据可视化 Python
python数据可视化 - matplotlib专题:带数据标签的双batch的Bar图绘制示例
python数据可视化 - matplotlib专题:带数据标签的双batch的Bar图绘制示例
35 0
|
7月前
使用Seaborn库创建图形的使用案例
使用Seaborn库创建图形的使用案例
37 0
|
6月前
|
数据可视化 Python
【100天精通Python】Day62:Python可视化_Matplotlib绘图基础,绘制折线图、散点图、柱状图、直方图和饼图,以及自定义图标外观和功能,示例+代码
【100天精通Python】Day62:Python可视化_Matplotlib绘图基础,绘制折线图、散点图、柱状图、直方图和饼图,以及自定义图标外观和功能,示例+代码
116 0
|
4月前
|
存储 数据可视化 定位技术
Python中matplotlib为多个列表数据绘制小提琴图
Python中matplotlib为多个列表数据绘制小提琴图
|
5月前
|
数据挖掘 Python
Python如何使用Matplotlib模块的pie()函数绘制饼形图?
Python如何使用Matplotlib模块的pie()函数绘制饼形图?
49 0
|
6月前
使用Plotly库创建图形的使用案例
使用Plotly库创建图形的使用案例
29 0
|
数据可视化 Python
可视化库Matplotlib-折线统计图
可视化库Matplotlib-折线统计图
可视化库Matplotlib-折线统计图
|
数据可视化 Linux API
Python中得可视化:使用Seaborn绘制常用图表(上)
Python中得可视化:使用Seaborn绘制常用图表
271 0
Python中得可视化:使用Seaborn绘制常用图表(上)
|
机器学习/深度学习 数据可视化 Python
Python中得可视化:使用Seaborn绘制常用图表(下)
Python中得可视化:使用Seaborn绘制常用图表
379 0
Python中得可视化:使用Seaborn绘制常用图表(下)
|
机器学习/深度学习 Python
自动美化你的Matplotlib ,使用Seaborn控制图表的默认值(二)
自动美化你的Matplotlib ,使用Seaborn控制图表的默认值(二)
184 0
自动美化你的Matplotlib ,使用Seaborn控制图表的默认值(二)