将 Quad 附加到 Bokeh 中的图形顶部

attach Quad to top of figure in Bokeh

由于 Bokeh 不支持 BoxAnnotations 的工具提示,我的解决方法是将四边形添加到图中并将 HoverTool 附加到四边形。无论缩放如何,我怎样才能使四边形始终从图形的顶部跨越到底部?

默认的 DataRange1d 范围有一个 renderers 属性,您可以将其设置为明确列出应自动调整范围的渲染器。在这种情况下,您可以将其设置为忽略四边形:

from bokeh.models import BoxAnnotation
from bokeh.plotting import figure, show

p = figure()

r = p.circle([1,2,3], [2,5,3])

p.quad(left=1.5, right=2.5, top=10e8, bottom=-10e8, alpha=0.2)

# add in all the renderers except the quad
p.y_range.renderers = [r]

show(p)