gpt4 book ai didi

python - 如何保护字形不被 TapTool 选择和更改?

转载 作者:行者123 更新时间:2023-11-28 19:04:43 25 4
gpt4 key购买 nike

我需要设置具有多个字形的单个 Bokeh 图形,并且只有字形的子集应该是可选的(即,在鼠标单击某个字形后触发操作)。经过多次试验和错误后,我找到了一种将字形的 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))

最佳答案

您可以手动创建 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 的列表

关于python - 如何保护字形不被 TapTool 选择和更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com