散景 - 通过点按工具保护字形以进行选择和更改

时间:2018-01-20 15:09:55

标签: python bokeh tap

我需要设置具有多个字形的单个散景图,并且只能选择字形的子集(即,在鼠标点击某些字形后触发动作)。经过多次试验和错误后,我找到了一种方法将字形的nonselection_glyph属性设置为None(参见下面的代码)。这是设置字形不可选择的最有效方法(或选择后禁用更改)吗?

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource

TOOLS = "tap"
p = figure(title="Some Figure", tools=TOOLS)

patches_source = ColumnDataSource(dict(x=[[1, 3, 2], [3, 4, 6, 6]], y=[[2, 1, 4], [4, 7, 8, 5]], alphas = [0.8, 0.3], colors=["firebrick", "navy"], name=['A', 'B']))
circles_source = ColumnDataSource(dict(x=[5, 7, 6], y=[2, 1, 4], alphas = [1.0, 1.0, 1.0], colors=["firebrick", "navy", "greeb"], name=['A', 'B', 'C']))

cglyph = p.circle(x='x', y='y', source=circles_source, size=25, line_width=2, alpha = 'alphas', color='colors')
pglyph = p.patches(xs='x', ys='y', source=patches_source, line_width=2, alpha = 'alphas', color='colors')
pglyph.nonselection_glyph = None #makes glyph invariant on selection


def callback_fcn(attr, old, new):
    if new['1d']['indices']:
        print('Newly selected: {}'.format(new['1d']['indices'][-1]))
    else:
        print('Nothing selected')
    print("{} - {} - {}".format(attr, old, new))

cglyph.data_source.on_change('selected',callback_fcn)
curdoc().add_root(column(p))

1 个答案:

答案 0 :(得分:0)

您可以手动创建TapTool并设置renderers属性,该属性将是您要使用该工具的渲染器列表:

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.models.tools import TapTool

p = figure(title="Some Figure", tools='')

patches_source = ColumnDataSource(dict(
    x=[[1, 3, 2], [3, 4, 6, 6]], y=[[2, 1, 4], [4, 7, 8, 5]],
    alphas = [0.8, 0.3], colors=["firebrick", "navy"],
    name=['A', 'B']
))
circles_source = ColumnDataSource(dict(
    x=[5, 7, 6], y=[2, 1, 4],
    alphas=[1.0, 1.0, 1.0], colors=["firebrick", "navy", "greeb"],
    name=['A', 'B', 'C']
))
cglyph = p.circle(
    x='x', y='y', source=circles_source, size=25,
    line_width=2, alpha='alphas', color='colors'
)
pglyph = p.patches(
    xs='x', ys='y', source=patches_source, 
    line_width=2, alpha = 'alphas', color='colors'
)

tap = TapTool(renderers=[cglyph])
tools = (tap)
p.add_tools(*tools)

def callback_fcn(attr, old, new):
    print("{} - {} - {}".format(attr, old, new))

cglyph.data_source.selected.on_change('indices',callback_fcn)
curdoc().add_root(column(p))

注意:或者,您可以提供renderer names

的列表