我不确定如何使用工具提示功能直接执行此 *** 作,但是这是一种使用文本字形模仿工具提示的方法:
from bokeh.io import gridplotfrom bokeh.plotting import figure, output_file, showfrom bokeh.models import ColumnDataSource, Circle, HoverTool, CustomJS, Textimport numpy as np(x, y, z) = np.arange(0, 100, 10), 100-np.arange(0, 100, 10), np.arange(0, 100, 10)/5output_file("hover_callback.html")p = figure(width=300, height=300, title='Hover over points', x_axis_label='x', y_axis_label='y')p.scatter(x, y)p2 = figure(width=300, height=300, title='Hover over points', x_axis_label='x', y_axis_label='z', x_range=p.x_range)p2.scatter(x, z)source = ColumnDataSource({'x': x, 'y': y, 'z': z, 'txt': ['x='+str(x[i])+', y='+str(y[i]) for i in range(len(x))], 'txt2': ['x='+str(x[i])+', z='+str(z[i]) for i in range(len(x))]})invisible_circle = Circle(x='x', y='y', fill_color='gray', fill_alpha=0.0, line_color=None, size=20) # size determines how big the hover area will beinvisible_circle2 = Circle(x='x', y='z', fill_color='gray', fill_alpha=0.0, line_color=None, size=20)invisible_text = Text(x='x', y='y', text='txt', text_color='black', text_alpha=0.0)visible_text = Text(x='x', y='y', text='txt', text_color='black', text_alpha=0.5)invisible_text2 = Text(x='x', y='z', text='txt2', text_color='black', text_alpha=0.0)visible_text2 = Text(x='x', y='z', text='txt2', text_color='black', text_alpha=0.5)cr = p.add_glyph(source, invisible_circle, selection_glyph=invisible_circle, nonselection_glyph=invisible_circle)crt = p.add_glyph(source, invisible_text, selection_glyph=visible_text, nonselection_glyph=invisible_text)cr2 = p2.add_glyph(source, invisible_circle2, selection_glyph=invisible_circle2, nonselection_glyph=invisible_circle2)cr2t = p2.add_glyph(source, invisible_text2, selection_glyph=visible_text2, nonselection_glyph=invisible_text2)pre = "source.set('selected', cb_data['index']);"callback = CustomJS(args={'source': source}, pre=pre)p.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[cr, crt]))p2.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[cr2, cr2t]))layout = gridplot([[p, p2]])show(layout)
输出如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)