Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set properties of selected/unselected glyphs in bokeh

I have a dataset consisting of timeseries of a few observables, and I'd like to use bokeh to look at the phase diagram at different points in the time series. What I want to know is how to change the properties of the selected or unselected glyphs (in this case I'd like to reduce the alpha of the unselected points or change the color of the selected ones).

The code below creates the interface I want in an ipython notebook, and is based on the example in the users guidehttp://docs.bokeh.org/en/latest/docs/user_guide/interaction/linking.html. I can't find any obvious options to set and I'd really rather not have to learn javascript for this one thing.

import numpy as np
from pandas import DataFrame
from bokeh.plotting import figure, output_notebook, show, gridplot
from bokeh.models import ColumnDataSource, widgets

def znzt_ts():#, plot_antisym=False, **kwargs):
    t = np.arange(1000)
    Eu = np.sin(t * np.pi/10) + np.random.random(1000)
    Ez = np.cos(t * np.pi/10) + np.random.random(1000)
    ts = DataFrame({'t': t, 'Eu': Eu, 'Ez': Ez})
    tools = 'box_zoom,pan,reset,save,box_select'
    source = ColumnDataSource(data=ts)
    original_source = ColumnDataSource(data=ts)

    p1 = figure(plot_width=300, plot_height=300, 
                tools=tools)
    p2 = figure(plot_width=300, plot_height=300, tools=tools,)
    p1.circle('t', 'Eu', source=source, size=1)
    p2.circle('Eu', 'Ez', source=source, size=1)
    return gridplot([[p1, p2]])

gp = znzt_ts()
output_notebook()
show(gp)
like image 755
Elliot Avatar asked Oct 18 '22 20:10

Elliot


1 Answers

This section shows you how to do this:

https://docs.bokeh.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs

You need to set the selection_glyph and nonselection_glyph of your circle renderers to glyphs with the desired properties.

In the manual, the renderer is accessed by assigning a name in the p.circle(..., name="mycircle") call and then using renderer = p.select(name="mycircle"). You could also save the reference to the renderer when it is returned by the p.circle(...) function: renderer = p.circle('t', 'Eu', source=source, line_color=None).

Once you have the reference to the renderer, you can assign the glyphs:

renderer.selection_glyph = Circle(fill_color='firebrick', line_color=None)
renderer.nonselection_glyph = Circle(fill_color='#1f77b4', fill_alpha=0.1, line_color=None)

import numpy as np
from pandas import DataFrame
from bokeh.plotting import figure, output_notebook, show, gridplot
from bokeh.models import ColumnDataSource, widgets
from bokeh.models.glyphs import Circle

def znzt_ts():#, plot_antisym=False, **kwargs):
    t = np.arange(1000)
    Eu = np.sin(t * np.pi/10) + np.random.random(1000)
    Ez = np.cos(t * np.pi/10) + np.random.random(1000)
    ts = DataFrame({'t': t, 'Eu': Eu, 'Ez': Ez})
    tools = 'box_zoom,pan,reset,save,box_select'
    source = ColumnDataSource(data=ts)
    original_source = ColumnDataSource(data=ts)

    selection_glyph = Circle(fill_color='firebrick', line_color=None)
    nonselection_glyph = Circle(fill_color='#1f77b4', fill_alpha=0.1, line_color=None)

    p1 = figure(plot_width=300, plot_height=300, tools=tools)
    r1 = p1.circle('t', 'Eu', source=source, line_color=None)
    r1.selection_glyph = selection_glyph
    r1.nonselection_glyph = nonselection_glyph


    p2 = figure(plot_width=300, plot_height=300, tools=tools)
    r2 = p2.circle('Eu', 'Ez', source=source, line_color=None)
    r2.selection_glyph = selection_glyph
    r2.nonselection_glyph = nonselection_glyph
    return gridplot([[p1, p2]])

gp = znzt_ts()
output_notebook()
show(gp)
like image 122
Jake Avatar answered Oct 23 '22 09:10

Jake