首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

为交互式数据可视化库Bokeh提供数据的N种方式

没有数据,自然就没办法进行数据可视化,这一部分就介绍提供数据的几种方式,此外,还会介绍筛选器的使用。

直接提供数据

在Bokeh中,可以将值列表直接传递给绘制图表。

from bokeh.plotting import figure

x_values = [1, 2, 3, 4, 5]

y_values = [6, 7, 2, 3, 6]

p = figure()

p.circle(x=x_values, y=y_values)

ColumnDataSource

ColumnDataSource是大多数Bokeh图的核心,用于提供可视化的数据,利用它,可以轻松地在多个图和小部件之间共享数据。当使用相同的ColumnDataSource来驱动多个渲染器时,也会共享数据源的选择。因此可以使用选择工具从一个绘图中选择数据点,并使它们在第二个绘图(链接选择)中自动高亮显示。

from bokeh.plotting import figure

from bokeh.models import ColumnDataSource

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

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

source = ColumnDataSource(data=data)

p = figure()

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

数据参数也可以是Pandas DataFrame或GroupBy对象。

source = ColumnDataSource(df)

group = df.groupby(('colA', 'ColB'))

source = ColumnDataSource(group)

数据流

ColumnDataSource流是将新数据附加到CDS的有效方式。 通过使用流方法,Bokeh仅向浏览器发送新数据,而不是整个数据集。

source = ColumnDataSource(data=dict(foo=[], bar=[]))

# 对数据源中所有进行新的,相同长度的更新

new_data = {

'foo' : [10, 20],

'bar' : [100, 200],}

source.stream(new_data)

修补

ColumnDataSource修补是更新数据源切片的有效方法。 通过使用补丁方法,Bokeh只需将新数据发送到浏览器而不是整个数据集。

(index, new_value)# 替换单个列值

# or

(slice, new_values)# 替换多个列值

使用CDSView筛选数据

通常,我们会需要关注从大数据中进行二次采样或筛选的一部分数据。 使用CDSView,底层数据不需要更改,并且可以在图中共享。 CDSView由一个或多个筛选器组成,用于选择应绑定到特定字形的数据源的行。

CDSView有两个属性,源和筛选器。

from bokeh.plotting import figure

from bokeh.models import ColumnDataSource, CDSView

source = ColumnDataSource(some_data)

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

p = figure()

p.circle(x="x", y="y", source=source, view=view)

索引筛选器

索引筛选器( IndexFilter)是最简单的筛选器类型,是一个你想要包含在图中数据的索引。

from bokeh.plotting import figure, output_file, show

from bokeh.models import ColumnDataSource, CDSView, IndexFilter

from bokeh.layouts import gridplot

output_file("index_filter.html")

source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[1, 2, 3, 4, 5]))

view = CDSView(source=source, filters=[IndexFilter([0, 2, 4])])tools = ["box_select", "hover", "reset"]

p = figure(plot_height=300, plot_width=300, tools=tools)

p.circle(x="x", y="y", size=10, hover_color="red", source=source)

p_filtered = figure(plot_height=300, plot_width=300, tools=tools)

p_filtered.circle(x="x", y="y", size=10, hover_color="red", source=source, view=view)

show(gridplot([[p, p_filtered]]))

布尔筛选器

布尔筛选器(BooleanFilter)通过布尔值属性中的True或False值列表从数据源中选择行。

from bokeh.plotting import figure, output_file, show

from bokeh.models import ColumnDataSource, CDSView, BooleanFilter

from bokeh.layouts import gridplot

output_file("boolean_filter.html")

source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[1, 2, 3, 4, 5]))

booleans = [True if y_val > 2 else False for y_val in source.data['y']]

view = CDSView(source=source, filters=[BooleanFilter(booleans)])

tools = ["box_select", "hover", "reset"]

p = figure(plot_height=300, plot_width=300, tools=tools)

p.circle(x="x", y="y", size=10, hover_color="red", source=source)

p_filtered = figure(plot_height=300, plot_width=300, tools=tools, x_range=p.x_range, y_range=p.y_range)

p_filtered.circle(x="x", y="y", size=10, hover_color="red", source=source, view=view)

show(gridplot([[p, p_filtered]]))

组筛选器

组筛选器可以从数据集中选择具有分类变量特定值的行。 组筛选器有两个属性,column_name,ColumnDataSource中的列名和组,要选择的列的值。

在下面的例子中,花朵包含一个分类变量物种,它可以是setosa,versicolor或virginica。

from bokeh.plotting import figure, output_file, show

from bokeh.layouts import gridplot

from bokeh.models import ColumnDataSource, CDSView, GroupFilter

from 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]]))

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180422G11ZT600?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券