您现在的位置是:首页 > 文章详情

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

日期:2018-09-25点击:391
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]

图示如下:

1dd5e77bc41b3dfa74a62981f95a7317338de365

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)

图示如下:

3bdecbfdf0bfc91f0b912b0f70973e56cbeb50f9

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)

图示如下:

1905d86438bad4aa430993dd334f7cae7778ab25
 
  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)

图示如下:

f01934c180a9e4cdb45387ff55afc361e48c2972

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

图示如下:

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


  2. g.describe()

图示如下:

47058cc4bffd9a87d1b326c4bb3e88eb3465a53a
 
  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)

图示如下:

460e062ef82ac83f96580174498ceb4773360d9d

3 小结

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


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


原文链接:https://yq.aliyun.com/articles/645338
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章