如何在bokeh中设置选定/未选定字形的属性 [英] How to set properties of selected/unselected glyphs in bokeh

查看:81
本文介绍了如何在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).

以下代码基于

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)

这篇关于如何在bokeh中设置选定/未选定字形的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆