如何在散景中设置 selected/unselected 字形的属性

How to set properties of selected/unselected glyphs in bokeh

我有一个数据集,其中包含一些可观测值的时间序列,我想使用散景来查看时间序列中不同点的相图。我想知道的是如何更改选定或未选定字形的属性(在这种情况下,我想减少未选定点的 alpha 或更改选定点的颜色)。

下面的代码在 ipython 笔记本中创建了我想要的界面,并且基于 users guidehttp://docs.bokeh.org/en/latest/docs/user_guide/interaction/linking.html 中的示例。我找不到任何明显的设置选项,我真的不想为这一件事学习 javascript。

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)

本节将向您展示如何执行此操作:

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

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

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

获得对渲染器的引用后,您可以分配字形:

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)

要完全删除未选中的字形,您可以使用:

fig = Figure()
c = fig.circle(x='x', y='y', source=source)
c.nonselection_glyph.visible = False