python - Show text annotations on selection in Bokeh -
i have little bokeh plot data points , associated text labels. want text labels appear when user selects points box select tool. gets me close:
from bokeh.plotting import columndatasource,figure,show source = columndatasource( data=dict( x=test[:,0], y=test[:,1], label=[unquote_plus(vocab_idx[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) this close, in if draw box around points, text labels shown , rest hidden, problem renders text labels start (which not want). initial plot should have labels hidden, , should appear upon box_select.
i thought start rendering alpha=0.0, , setting selection_glyph parameter, this:
... 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. ... but throws error:
attributeerror: 'nonetype' object has no attribute 'text_alpha' when trying access text_alpha attribute of selection_glyph.
i know use hover effect here or similar, need labels default not being visible. alternative, not ideal, solution have toggle button switches labels on , off, i'm not sure how either.
what doing wrong here?
as of version 0.11.1, value of selection_glyph none default. interpreted bokehjs "don't different, draw glyph normal". need create selection_glyph. there 2 ways this, both demonstrated here:
http://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs
basically, are
by hand
create actual circle bokeh model, like:
selected_circle = circle(fill_alpha=1, fill_color="firebrick", line_color=none) renderer.selection_glyph = selected_circle or
using glyph method parameters
alternatively, convenience figure.circle accepts paramters selection_fill_alpha or selection_color (basically line or fill or text property, prefixed selection_) :
p.circle(..., selection_color="firebrick") then circle created automatically , used renderer.selection_glyph
i hope useful information. if so, highlights there 2 possible ways project improved:
updating docs explicit , highlight
renderer.selection_glyphnonedefaultchanging code
renderer.selection_glyphcopy ofrenderer.glyphdefault (then original code work)
either small in scope , ideal new contributor. if interested in working pull request either of these tasks, (and other users) grateful contribution. in case, please make issue first @
https://github.com/bokeh/bokeh/issues
that references discussion, , can provide more details or answer questions.
Comments
Post a Comment