Search code examples
pythonbokehstyling

Anchor/Fix a text in Bokeh to a figure's bounds


Is there a way to fix/anchor a text in a Bokeh figure to the active viewport? Meaning that panning/zooming will not alter its position. This corresponds to the position: fixed option in CSS.

Should look something like:

 p.text(x="figure.top", y="figure.left", text="some text", x_offset=10, y_offset=-10)

Thanks!


Solution

  • I don't think it's possible with the Text glyph, but if you don't need a real glyph then you can use Label since it allows you to specify the units for its coordinates:

    from bokeh.io import show
    from bokeh.models import Label
    from bokeh.plotting import figure
    
    p = figure()
    p.add_layout(Label(x=0, x_units='screen', y=0, y_units='screen', text='Hello'))
    
    show(p)