Help on BokehJS/CustomJS to update plot from pandas dataframe?

One of the benefits of a “building block” architecture is that is makes software development easier in some ways, and another is that it make it flexible for users to put things together in lots of novel configurations. But there are also costs, chiefly that the a combinatorial explosion of usage possibilities makes both documentation and testing more difficult. The two features (CDS Views, and “grouped” legends) were developed at very different times, by different people, and the most likely explanation is that no-one has ever yet considered how they might interact together, or made sure that they behave in some desirable way that is maintained under test.

TLDR; this probably just needs to go in a GitHub issues (with a very minimal complete test script to demonstrate the issue)

Here is a example I found on bokeh with the issue I have. Legend group shows both labels with same color even though I am just plotting “lo”.
I’ll add this to github

from bokeh.io import show
from bokeh.models import ColumnDataSource, GroupFilter, CDSView
from bokeh.palettes import RdBu3
from bokeh.plotting import figure

c1 = RdBu3[2] # red
c2 = RdBu3[0] # blue
source = ColumnDataSource(dict(
    x=[1, 2, 3, 4, 5, 6],
    y=[2, 1, 2, 1, 2, 1],
    color=[c1, c2, c1, c2, c1, c2],
    label=['hi', 'lo', 'hi', 'lo', 'hi', 'lo']
))

p = figure(x_range=(0, 7), y_range=(0, 3), plot_height=300, tools='save')

# legend field matches the column in the source
p.circle( x='x', y='y', radius=0.5, color='color', legend_group='label', source=source, view = CDSView(source=source, filters=[GroupFilter(column_name='label', group='lo')]))

show(p)
1 Like

@Zana I have replied on the issue, but also for anyone here:

TLDR; it will never be possible to make legend_group take a CDS view into account because legend_group does grouping on the Python side (that is specifically why it exists), but the filtering does not happen until the JavaScript side, in the browser.

There is a legend_field that also performs grouping, but on the JS side, and so there is some hope of improvement there. (I had actually erroneously assumed the question was about legend_field) The tradeoff at least currently is that there less control, e.g. over legend item ordering, since you do not have access to individual legend items up front on the Python side.