如何在bokeh中设置选定/未选定字形的属性

问题描述:

我有一个由几个可观察到的时间序列组成的数据集,我想用bokeh观察时间序列中不同点的相位图.我想知道的是如何更改选定或未选定字形的属性(在这种情况下,我想减小未选定点的alpha或更改选定点的颜色).

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).

以下代码基于 http://docs.bokeh.org/en/latest/docs/user_guide/interaction/linking.html .我找不到任何明显的选项可以设置,我真的很想不必为此学习javascript.

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)

本节向您展示如何做到这一点:

This section shows you how to do this:

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

您需要将圆形渲染器的selection_glyphnonselection_glyph设置为具有所需属性的字形.

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

在手册中,通过在p.circle(..., name="mycircle")调用中分配一个名称,然后使用renderer = p.select(name="mycircle")来访问渲染器.当p.circle(...)函数:renderer = p.circle('t', 'Eu', source=source, line_color=None)返回时,您也可以保存对渲染器的引用.

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)