Bokeh use of Column Data Source and Box_Select

2024/10/12 2:29:08

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_rangep.scatter(x=x0, y=y0, marker='square', size=1, fill_color='red', source=self.source)p.title.text = filenamep.title.text_color = 'orange'p.xaxis.axis_label = 'T'p.yaxis.axis_label = y_labelp.xaxis.minor_tick_line_color = 'red'p.yaxis.minor_tick_line_color = Nonereturn 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.

Answer

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, showsource = 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://en.xdnf.cn/q/118248.html

Related Q&A

How Does a Pyqtgraph Export for Three Subplots Look Like?

Using PyQtGraph, I would like to generate three sub plots in one chart and export this chart to a file.As I will repeat this a lot of times, it is quite performance sensitive. Therefore I do not need t…

Use class variables as instance vars?

What I would like to do there is declaring class variables, but actually use them as vars of the instance. I have a class Field and a class Thing, like this:class Field(object):def __set__(self, instan…

Get amount from django-paypal

I am using django-paypal to receive payment. I am currently paying as well as receiving payment using sandbox accounts. The payment procedure seems to be working fine. My problem is once I get back the…

Python get file regardless of upper or lower

Im trying to use this on my program to get an mp3 file regardless of case, and Ive this code:import glob import fnmatch, redef custom_song(name):for song in re.compile(fnmatch.translate(glob.glob("…

how to save h5py arrays with different sizes?

I am referring this question to this. I am making this new thread because I did not really understand the answer given there and hopefully there is someone who could explain it more to me. Basically my…

Cannot allocate memory on Popen commands

I have a VPS server with Ubuntu 11.10 64bit and sometimes when I execute a subprocess.Popen command I get am getting too much this error:OSError: [Errno 12] Cannot allocate memoryConfig details: For ea…

Python - find where the plot crosses the axhline on python plot

I am doing some analysis on some simple data, and I am trying to plot auto-correlation and partial auto-correlation. Using these plots, I am trying to find the P and Q value to plot in my ARIMA model.I…

remove tick labels in Python but keep gridlines

I have a Python script which is producing a plot consisting of 3 subplots all in 1 column.In the middle subplot, I currently have gridlines, but I want to remove the x axis tick labels.I have triedax2.…

Signal in PySide not emitted when called by a timer

I need to emit a signal periodically. A timer executes certain function, which emits the signal that I want. For some reason this function is not being emitted. I was able to reproduce the error on min…

pybuilder and pytest: cannot import source code when running tests

so i have a project:<root> |- src|-main|-python|-data_merger|- common|- constans|- controller|- resources|- rest|-tests|-unittest|-integrationtestdata_merger is marked as root (I am using Pycharm…