如何修改Bokeh中CrosshairTool十字光标的显示颜色
修改Bokeh CrosshairTool十字光标颜色的实现方法
可以直接通过CrosshairTool自带的样式属性修改十字光标颜色,无需自定义扩展组件。
常用可配置的样式属性如下:
line_color:统一设置横竖十字线的颜色,支持传入十六进制色值、RGB元组、Bokeh内置命名色值x_line_color:单独设置垂直方向(对应X轴位置)十字线的颜色y_line_color:单独设置水平方向(对应Y轴位置)十字线的颜色- 配套可调整
line_width(线宽)、line_alpha(透明度)、line_dash(线型)等属性自定义光标样式
基于你提供的示例代码,修改后的可运行版本如下:
import numpy as np from bokeh.plotting import figure, show from bokeh.layouts import gridplot from bokeh.models import CrosshairTool plots = [figure() for i in range(6)] [plot.line(np.arange(10), np.random.random(10)) for plot in plots] # 初始化带自定义颜色的十字光标,这里示例设置为蓝色、线宽1.5 linked_crosshair = CrosshairTool( dimensions="both", line_color="#2196F3", line_width=1.5 ) # 若需要横竖线设置不同颜色,可替换为如下初始化写法 # linked_crosshair = CrosshairTool( # dimensions="both", # x_line_color="red", # 垂直方向线为红色 # y_line_color="green", # 水平方向线为绿色 # line_width=1 # ) for plot in plots: plot.add_tools(linked_crosshair) show(gridplot(children=[plot for plot in plots], ncols=3))
注意:
x_line_color、y_line_color单独配置属性仅支持Bokeh 2.0及以上版本,低版本直接使用line_color统一设置颜色即可正常兼容。
内容的提问来源于stack exchange,提问作者David H. J.
相关产品推荐
相关产品推荐

