1
votes

I'm reasonably experienced with Python and Pandas, but very new to the Bokeh package and have been struggling with this task for the last couple days without progress. I'm building a dashboard to display data, and it would be very helpful to use radio buttons to select/show different lines on the same plot. I've been following the example from here (https://github.com/bokeh/bokeh/issues/3715) which I can get to work fine using the checkboxes. I've changed the "CheckboxGroup" to a "RadioGroup" in line 21, as well as the "active" argument. The result is that both plotted lines disappear when I change the selection of the radio buttons and never come back. I don't understand why the CheckboxGroup works and the RadioGroup doesn't, considering how similar they are and that they both use the "active" event for callback. Could somebody please point out my mistake?

import numpy as np

from bokeh.io import show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import CheckboxGroup, RadioGroup
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.layouts import column, row
from bokeh.plotting import figure

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(3*np.pi*t)
c = np.cos(3*np.pi*t)

source = ColumnDataSource(data=dict(t=t, s=s, c=c))

plot = figure(plot_width=400, plot_height=400)
a = plot.line('t', 's', source=source, line_width=3, line_alpha=0.6, 
line_color='blue')
b = plot.line('t', 'c', source=source, line_width=3, line_alpha=0.6,
line_color='red')

checkbox = RadioGroup(labels=["Cosinus", "Sinus"], active=0)

checkbox.callback = CustomJS(args=dict(line0=a, line1=b), code="""   
    //console.log(cb_obj.active);
    line0.visible = false;
    line1.visible = false;
    for (i in cb_obj.active) {
        //console.log(cb_obj.active[i]);
        if (cb_obj.active[i] == 0) {
            line0.visible = true;
        } else if (cb_obj.active[i] == 1) {
            line1.visible = true;
        }
    }
""")

layout = row(plot, widgetbox(checkbox))

show(layout)
3

3 Answers

1
votes

In radioboxgroup, you can only have one active value at once so your for loop is useless as you try looping over an integer. Here is your code corrected

checkbox.callback = CustomJS(args=dict(line0=a, line1=b), code="""   
    line0.visible = false;
    line1.visible = false;

    if (cb_obj.active == 0) {
        line0.visible = true;
    } else if (cb_obj.active == 1) {
        line1.visible = true;
    }
""")

There is a better way to do this, if you have many lines you can use the length of the "labels" list in a loop

line_list = [line0,line1];

lab_len=cb_obj.labels.length;

for (i=0;i<lab_len;i++) {
if (cb_obj.active == i) {
line_list[i].visible = true;
} else {
line_list[i].visible = false;
}
}

In a checkboxgroup the "active" attribute is a list of indices. You just have to change the if condition to make the code work:

checkbox = CheckboxGroup(labels=["Cosinus", "Sinus"], active=[0,1])

checkbox.callback = CustomJS(args=dict(line0=a, line1=b), code="""   
    line_list = [line0,line1];

    lab_len=cb_obj.labels.length;

    for (i=0;i<lab_len;i++) {
    if (cb_obj.active.includes(i)) {
    line_list[i].visible = true;
    } else {
    line_list[i].visible = false;
    }
    }
""")
0
votes

I think it is easier to plot this using the multi_line method. Then you can chose which line(s) are selected on a callback as shown below.

import numpy as np

from bokeh.io import show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import CheckboxGroup, RadioGroup
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.layouts import column, row
from bokeh.plotting import figure

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(3*np.pi*t)
c = np.cos(3*np.pi*t)

source = ColumnDataSource(data=dict(t=[t]*2, s=[s,c], colors = ['red', 'blue']))

plot = figure(plot_width=400, plot_height=400)
plot.multi_line('t', 's',line_color='colors' ,source=source)

checkbox = RadioGroup(labels=["Cosinus", "Sinus"], active=0)

checkbox.callback = CustomJS(args=dict(s=source), code="""   
    s.selected['1d'].indices = [cb_obj.active]
    s.trigger('change');
""")

layout = row(plot, widgetbox(checkbox))

show(layout)
0
votes

If you are trying to let users easily control the visibility of glyphs, then as of Bokeh 0.12.5 you can have legends be interactive, to either hide glyphs or mute them, when user clicks on a legend entry:

enter image description here

If you want to completely hide glyphs, set a legend's click_policy to "hide":

p = figure()
p.circle([1,2,3], [4,5,6], legend="foo")
p.square([1,2,3], [7,9,8], legend="bar")
p.legend.click_policy = "hide"

You can also set a policy to mute:

p.legend.click_policy = "mute"

But you will need to specify what the "muted" appearance looks like for each glyph, e.g.

p.circle([1,2,3], [4,5,6], muted_alpha=0.2, legend="foo")