javascript - 如何在 Bokeh 中设置选定/未选定字形的属性

标签 javascript python bokeh

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

下面的代码在 ipython notebook 中创建了我想要的界面,并且基于 users guide 中的示例。 http://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 来访问渲染器(名称=“我的圈子”)。您还可以在 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)

关于javascript - 如何在 Bokeh 中设置选定/未选定字形的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210614/

相关文章:

javascript - 描述符可以更改 Typescript 中的类型吗?

python - 运行python脚本安装Windows服务时没有响应

python - QHBarModelMapper 和 QAbstractItemModel 行编号

python - 为什么我从 HoloViews 保存的 Datashader 绘图的分辨率这么低?

c# - 对 asp.net mvc 应用程序的长轮询需要 POST 而不是 GET ajax 方法。为什么?

javascript - 是否可以确定 dom 元素何时进入 View ?

javascript - 为鼠标悬停链接添加 Fadein 到现有 CSS

python - 写入在第二个函数中打开的文件 (Python)

python - 在 Bokeh 中的条形图顶部添加误差线

python - export_png 所有图层的 Bokeh