且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-22 19:43:31

Bokeh 系列文章传送门:

前面,我们分享了关于 bokeh 入门 、 figure 使用、以及bokeh基础图形介绍的内容。今天,我们在前文的基础上,主要来分享Bokeh中数据的使用方式,尤其是 Bokeh 特有的数据类型 ColumnDataSource 的使用。

本文主要内容如下:

  • 1 直接提供数据

  • 2 通过 ColumnDataSource 来提供数据

    • 2.1 data 为字典

    • 2.2 data 为 pandas 的 DataFrame

    • 2.3 data 为 pandas 的 DataFrame 的 groupby 对象

本文的环境为

  • window 7 系统

  • python 3.6

  • Jupyter Notebook

  • bokeh 0.13.0

数据是进行数据可视化的必要基础, 在 bokeh 中,数据有几种呈现方式。

(1)直接提供数据

(2)通过 ColumnDataSource 来提供数据

1 直接提供数据

首先加载相关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()

可以通过数据列的形式(list)直接提供数据


  1. np.random.seed(15)


  2. x=np.random.randint(1,20,size=6)

  3. y=np.random.randint(20,50,size=6)


  4. print(x)

  5. print(y)


  6. p = figure(plot_width=300, plot_height=300)

  7. p.circle(x, y,size=y)


  8. show(p)


  1. [ 9 13 6 1 8 12]

  2. [41 42 35 49 37 33]

图示如下:

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

2 通过 ColumnDataSource 来提供数据

ColumnDataSource 是 Bokeh 中一种重要的数据形式,ColumnDataSource() 方法有一个参数为 “data”, “data”主要有以下三种类型:

(1)data 为字典

(2)data 为 Pandas 的 DataFrame

(3)data 为 Pandas 的 DataFrame 的 groupby 对象

2.1 data 为字典

data 的表现形式是一个字典的形式, 一般情况下, 字典的 key 值是一个字符串,代表列名称, 而 value则是 list形式 或者 numpy的 array 形式。 演示如下:


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

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


  3. source = ColumnDataSource(data=data)

  4. source

ColumnDataSource(

id = '0d38f463-7107-49e7-9263-7b5395afc00a', …)


  1. type(source)


  1. bokeh.models.sources.ColumnDataSource

从上面结果来看, source 是一个 ColumnDataSource 对象,不能直接打印出来,后续可以在绘图是传入参数进行使用。


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

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


  3. source = ColumnDataSource(data=data)


  4. p = figure(plot_width=300, plot_height=300)

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

  6. show(p)

图示如下:

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

2.2 data 为 pandas 的 DataFrame

ColumnDataSource (简称为 CDS) 的 data 参数,也可以是 pandas 的 DataFrame。 当CDS的参数是 DataFrame 时,参数中可以直接用 DataFrame 的列名称,索引名称,也可以直接用DataFrame 已有的名称, 如果没有索引名称,默认情况下, 索引名称 用 "index" 就可以。


  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


x_column y_column
0 1 6
1 2 7
2 9 2
3 4 3
4 5 6
5 8 2

  1. source_df = ColumnDataSource(df)


  2. p = figure(plot_width=300, plot_height=300)

  3. p.circle(x='x_column', y='y_column', source=source_df, size=15)

  4. show(p)

图示如下:

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

  1. source_df = ColumnDataSource(df)


  2. p = figure(plot_width=300, plot_height=300)


  3. # 使用 “index” 作为 DataFrame 的默认索引名称

  4. p.circle(x= 'index', y='y_column', source=source_df, size=15)

  5. show(p)

图示如下:

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

2.3 data 为 pandas 的 DataFrame 的 groupby 对象

ColumnDataSource (简称为 CDS) 的 data 参数,还也可以是 pandas 的 DataFrame 的 groupby 对象。

当CDS的参数是 DataFrame 的 groupby 对象时,在绘图时使用的 列名为 groupby 对象的 groupby.describe() 方法中的 列名称。

由于 groupby 会有多个统计参数,在引用时, 列表会合并到一起,形式如: column_mean 等。


  1. dates = pd.date_range('20180101', periods=360)


  2. df = pd.DataFrame(np.random.randn(360,2), index=dates, columns=list('AB'))


  3. df['C'] = ['Good', 'Bad', 'Common', 'Good','Good']*72

  4. df['month'] = df.index.month


  5. df

图示如下:

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

  1. g = df.groupby('month')


  2. g.describe()

图示如下:

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

  1. source_g = ColumnDataSource(g)

  2. p = figure(plot_width=400, plot_height=300)

  3. p.vbar(x='month', width=0.3, bottom=0, top='A_mean',source=source_g)

  4. show(p)

图示如下:

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

3 小结

相对于 matplotlib, pandas,seaborn 等 Python 绘图库, Bokeh 提供了特有的数据源,掌握好 ColumnDataSource 的应用,对于 Bokeh 绘图是至关重要的。 后续,我们还会陆续接触到 ColumnDataSource 的相关用法。


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