如何使用bokeh将vbar与圆形图链接起来? [英] how to link vbar with circle plots using bokeh?

查看:98
本文介绍了如何使用bokeh将vbar与圆形图链接起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个基于相同数据集的图.如何链接所有三个图,以便当我在vbar图中选择某个物种时,两个散点图也更改为仅该物种中的图点.

I have three plots based on the same dataset. How can I link all three plots so that when I select a certain species in vbar plot, two scatter plot also change to plot points in that species only.

不胜感激〜

from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, CategoricalColorMapper
from bokeh.layouts import column, row

#color mapper to color data by species
mapper = CategoricalColorMapper(factors = ['setosa','versicolor', 'virginica'],\
                                 palette = ['green', 'blue', 'red'])


output_file("plots.html")

#group by species and plot barplot for count
species = flowers.groupby('species')

source = ColumnDataSource(species)

p = figure(plot_width = 800, plot_height = 400, title = 'Count by Species', \
           x_range = source.data['species'], y_range = (0,60),tools = 'box_select')

p.vbar(x = 'species', top = 'petal_length_count', width = 0.8, source = source,\
       nonselection_fill_color = 'gray', nonselection_fill_alpha = 0.2,\
       color = {'field': 'species', 'transform': mapper})

labels = LabelSet(x='species', y='petal_length_count', text='petal_length_count', 
              x_offset=5, y_offset=5, source=source)

p.add_layout(labels)



#scatter plot for sepal length and width
source1 = ColumnDataSource(flowers)
p1 = figure(plot_width = 800, plot_height = 400, tools = 'box_select', title = 'scatter plot for sepal')

p1.circle(x = 'sepal_length', y ='sepal_width', source = source1, \
          nonselection_fill_color = 'gray', nonselection_fill_alpha = 0.2, \
          color = {'field': 'species', 'transform': mapper})


#scatter plot for petal length and width
p2 = figure(plot_width = 800, plot_height = 400, tools = 'box_select', title = 'scatter plot for petal')

p2.circle(x = 'petal_length', y ='petal_width', source = source1, \
          nonselection_fill_color = 'gray', nonselection_fill_alpha = 0.2, \
          color = {'field': 'species', 'transform': mapper})


#show all three plots
show(column(p, row(p1, p2)))

推荐答案

我认为目前尚无此功能.但是您可以通过CustomJS回调显式链接两个ColumnDataSource:

I don't think there's some functionality existing for this at the moment. But you can explicitly link two ColumnDataSources with a CustomJS callback:

from bokeh.models import CusomJS

source = ColumnDataSource(species)
source1 = ColumnDataSource(flowers)
source.js_on_change('selected', CustomJS(args=dict(s1=source1), code="""
    const indices = cb_obj.selected['1d'].indices;
    const species = new Set(indices.map(i => cb_obj.data.species[i]));
    s1.selected['1d'].indices = s1.data.species.reduce((acc, s, i) => {if (species.has(s)) acc.push(i); return acc}, []);
    s1.select.emit();
"""))

请注意,此回调仅使从条形图到散点图的选择同步.为了使散点图上的选择影响条形图,您必须编写一些其他代码.

Note that this callback only synchronizes selection from the bar plot to the scatter plots. To make selections on the scatter plots influence the bar plot, you'll have to write some additional code.

这篇关于如何使用bokeh将vbar与圆形图链接起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆