Bokeh use of Column Data Source and Box_Select

依然范特西╮ 提交于 2019-12-24 21:26:01

问题


I'm lost as to how to set up a Column Data Source so that I can select points from one graph and have the corresponding points highlighted in another graph. I am trying to learn more about how this works.

The sample code I am using is the example called Linked Brushing. I'd like to see if I can get the same effect with my own code, below. That web page explanation also refers to Linked Selection with Filtered Data but I don't understand what the code filters=[BooleanFilter([True if y > 250 or y < 100 else False for y in y1] on that page does, so I'm not sure how to adapt it, or if it's even relevant.

Here is my code:

from bokeh.plotting import figure, output_file, show, Column
from bokeh.models import ColumnDataSource, CDSView, BooleanFilter
from MyFiles import *

class bokehPlot:
    def __init__(self, filename, t, a, b, c, d):

        self.source = ColumnDataSource(data=dict(x=t, y1=a, y2=b, y3=c, y4=d))

        p1 = self.makePlot(filename, 'x', 'y1', 'A')
        p2 = self.makePlot(filename, 'x', 'y2', 'B', x_link=p1)
        p3 = self.makePlot(filename, 'x', 'y3', 'C', x_link=p1)
        p4 = self.makePlot(filename, 'x', 'y4', 'D', x_link=p1)

        output_file('scatter_plotting.html', mode='cdn')

        p = Column(p1, p2, p3, p4)
        show(p)

    def makePlot(self,filename,x0,y0,y_label, **optional):

        TOOLS = "box_zoom,box_select,reset"

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

        if ('x_link' in optional):
            p0 = optional['x_link']
            p.x_range = p0.x_range

        p.scatter(x=x0, y=y0, marker='square', size=1, fill_color='red', source=self.source)
        p.title.text = filename
        p.title.text_color = 'orange'
        p.xaxis.axis_label = 'T'
        p.yaxis.axis_label = y_label
        p.xaxis.minor_tick_line_color = 'red'
        p.yaxis.minor_tick_line_color = None
        return p

And my main looks like this (set to pass along up to 100K data points from the file):web

p = readMyFile(path+filename+extension, 100000)
t = p.time()
a = p.a()
b = p.b()
c = p.c()
d = p.d()
v = bokehPlot(filename, t, a, b, c, d)

The variables t, a, b, c, and d are type numpy ndarray.

I've managed to link the plots so I can pan and zoom them all from one graph. I would like to grab a cluster of data from one plot and see them highlighted, along with the corresponding values (at the same t values) highlighted on the other graphs.

In this code, I can draw a selection box, but it just remains for a moment, then disappears, and I see no effect on any plot. How is the box_select linked to the source and what causes the plots to redraw?

This is just one step in trying to familiarize myself with Bokeh. My next goal will be to use TSNE to cluster my data and show the clusters with synchronized colors in each graph. But first, I want to understand the mechanics of using the column data set here. In the sample code, for example, I don't see any explicit connection between the box_select operation and the source variable and what causes the plot to redraw.


回答1:


My understanding is that the BooleanFilter, the IndexFilter and the GroupFilter can be used to filter the data in one of your plots before rendering. If you only want the second plot to respond to events in the first plot then you should just use gridplot as suggested in the comment. As long as the plots have the same ColumnDataSource they should be linked.

from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show

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

tools = ["box_select", "hover", "reset"]
p_0 = figure(plot_height=300, plot_width=300, tools=tools)
p_0.circle(x="x", y="y", size=10, hover_color="red", source=source)

p_1 = figure(plot_height=300, plot_width=300, tools=tools)
p_1.circle(x="x", y="z", size=10, hover_color="red", source=source)

show(gridplot([[p_0, p_1]]))


来源:https://stackoverflow.com/questions/53365100/bokeh-use-of-column-data-source-and-box-select

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!