我有一个带有数据点和相关文本标签的小散景图 . 我想要的是文本标签仅在用户使用框选择工具选择点时出现 . 这让我很接近:
from bokeh.plotting import ColumnDataSource,figure,show
source = ColumnDataSource(
data=dict(
x=test[:,0],
y=test[:,1],
label=[unquote_plus(vocab_idx[i]) for i in range(len(test))]))
TOOLS="box_zoom,pan,reset,box_select"
p = figure(plot_width=400, plot_height=400,tools=TOOLS)
p.circle(x='x',y='y', size=10, color="red", alpha=0.25,source=source)
renderer = p.text(x='x',y='y',text='label',source=source)
renderer.nonselection_glyph.text_alpha=0.
show(p)
这很接近,因为如果我在某些点周围绘制一个框,则会显示这些文本标签,其余部分是隐藏的,但问题是它会渲染所有文本标签(这不是我想要的) . 初始绘图应隐藏所有标签,并且它们应仅出现在box_select上 .
我想我可以先用alpha = 0.0渲染一切,然后设置一个selection_glyph参数,如下所示:
...
renderer = p.text(x='x',y='y',text='label',source=source,alpha=0.)
renderer.nonselection_glyph.text_alpha=0.
renderer.selection_glyph.text_alpha=1.
...
但这会引发错误:
AttributeError: 'NoneType' object has no attribute 'text_alpha'
尝试访问 selection_glyph
的 text_alpha
属性时 .
我知道我可以在这里或类似地使用悬停效果,但需要标签默认为不可见 . 另一种但不理想的解决方案是使用切换按钮来打开和关闭标签,但我不知道该怎么做 .
我在这做错了什么?
1 回答
从版本
0.11.1
开始,selection_glyph
的值默认为None
. 这被BokehJS解释为"don't do anything different, just draw the glyph as normal" . 所以你需要实际创建一个selection_glyph
. 有两种方法可以做到这一点,这里都展示了:http://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs
基本上,他们是
手工
创建一个实际的
Circle
Bokeh模型,类似于:要么
使用字形方法参数
或者,为方便起见
Figure.circle
接受selection_fill_alpha
或selection_color
等参数(基本上是任何行或填充或文本属性,前缀为selection_
):然后将自动创建
Circle
并用于renderer.selection_glyph
我希望这是有用的信息 . 如果是这样,它强调可以通过两种方式改进项目:
将文档更新为显式,并突出显示
renderer.selection_glyph
默认为None
更改代码,以便
renderer.selection_glyph
默认只是renderer.glyph
的副本(那么您的原始代码将起作用)两者的范围都很小,非常适合新的贡献者 . 如果您有兴趣处理Pull Request以执行这些任务中的任何一项,我们(和其他用户)肯定会感谢您的贡献 . 在这种情况下,请先解决问题
https://github.com/bokeh/bokeh/issues
引用此讨论,我们可以提供更多详细信息或回答任何问题 .