如何在Bokeh中设置字形的悬停颜色?

时间:2019-01-09 20:50:27

标签: python bokeh

有两种在Bokeh中添加字形的方法。我更喜欢第二种方式,因为它为悬停和图例提供了更大的灵活性。我找不到在第二种方法中设置悬停颜色的选项。有办法吗?

from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, CDSView, IndexFilter
from bokeh.plotting import figure, show
from bokeh.io import curdoc, output_notebook, output_file, export_png    
from bokeh.models import (
  ColumnDataSource, Circle, HoverTool,Grid, TapTool,PanTool, WheelZoomTool, BoxSelectTool,ZoomInTool, ZoomOutTool, CDSView, GroupFilter)

curdoc().clear()
output_notebook()

source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[1, 2, 3, 4, 5]))
view = CDSView(source=source, filters=[IndexFilter([0, 2, 4])])

#---------------------Method1-----------------------------
tools = ["box_select", "hover", "reset"]
p = figure(plot_height=300, plot_width=300, tools=tools)
p.circle(x="x", y="y", size=10, hover_color="red", source=source)

#---------------------Method2-----------------------------
p_glypg = figure(plot_height=300, plot_width=300, tools="pan,wheel_zoom,box_zoom,reset,zoom_in,zoom_out,save")
circle = Circle(x="x", y="y", size=10)
c = p_glypg.add_glyph(source, circle)
c_hover = HoverTool(renderers=[c], tooltips=[('x', '@x')])
p_glypg.add_tools(c_hover) 

show(gridplot([[p,p_glypg]]))

enter image description here

1 个答案:

答案 0 :(得分:1)

可以按照较低的级别添加与选定/未选定相同的方式,如文档中所述:

https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#selected-and-unselected-glyphs

circle = Circle(x="x", y="y", size=10)
hover_circle = Circle(x="x", y="y", size=10, fill_color="red")
c = p_glypg.add_glyph(source, circle, hover_glyph=hover_circle)