Bokeh vbar with group counts after filtering with a CDSView

I’d like to create a Bokeh vertical bar chart (vbar) coupled with a scatter plot. I need to start from a given ColumnDataSource (then filtered with a CDSView), as this is only a part of a complex visualization and I need all the plots to be linked together.

The only tricky part for which I didn’t find anything about after extensive research is: how to show counts (i.e., the values provided in the top parameter of vbar) based only on data points that are both filtered AND selected in the scatter plot? Both the plots receive the same filters from which a view is created.

The documentation shows how to link selected points in different plots (by using the same ColumnDataSource object), how to filter them, and how to use vbar, but not all of them together.

Any hint? Thank you very much in advance.

Label/labelsets etc. are not glyphs but layouts… I’ve found this to be a little limiting/cumbersome at times but my go to workaround has been to make a separate source for the labels with the same indices as the main datasource, and use a customJS callback to trigger whenever a filter or selection gets applied, and have that JS callback iterate through the indices of your main source, and update the label datasource data accordingly with only the values of the indices of the main source that (in your case) are both selected and filtered.

Interested if there are better suggestions than this one…

Thank you @gmerritt123 for your response. Actually I’m not trying to use labels but somehow I need to count rows (after a groupby) to be shown in the vbar. So yesterday I came up with a similar idea to what you are proposing (multiple sources, callbacks to make the updates consistently) but I’m struggling a bit. Do you have some code to share as an example? Thank you very much!

Is the groupby happening each time a new filter gets applied… i.e. are you aggregating in a callback? How are you implementing the callbacks, with standalone JS or Bokeh serve?

Either way, I’m guessing all you’re needing to do is update the “top” parameter of the vbar with the results of the callback?

Writing out and posting a minimal reproducible working example has been most productive in terms of distilling for me the core of what i’m trying to do and in illustrating to others my exact question/roadblock. Oftentimes by the time I’ve written out my example I find the answer myself :smiley:

I guess what Alessia is trying (as well as me) is something to this: Selection Histogram — Altair 4.2.0rc1 documentation

So probably you would need to define a CDS-View like here (First steps 8: Providing and filtering data — Bokeh 2.4.2 Documentation) but not with a filter, but a groupby-sum action, which I find nowhere. Any ideas how to implement a groupby-sum action on a data source / view?

If you’re staying with CustomJS/Standalone, you’ll need to write some javascript that does the aggregation, or find some javascript that does it on the internet. If you’re using bokeh-serve you can get your server to do the work in python so your standard pandas groupby–>agg will do the job.

I’ve been using d3js to do it with CustomJS but there are probably simpler ways out there.

I am using bokeh serve and would prefer to run it in python. How ever I did not understand how I could integrate some python filtering function. You know any examples how I could get my server doing?

Unfortunately bokeh serve is not something I use. Hopefully someone else can chime in.