Update Location of Marker on Plot

I’m using an older version of Bokeh (3.11.6). I have a marker on a plot and I would like to update the location of the marker when the user selects a different value.

The update() function seems like the most straightforward way to go, but I’m not quite sure how to use it. Here’s what I’ve tried.

from bokeh.plotting import figure
from bokeh.models import Range1d

import panel as pn
pn.extension()

x = list(range(1,10))
y = list(1 for i in range(1,10)) 

p = figure(
    height=130,
    width=1800)
p.line(x=x, y=y)

int_input = pn.widgets.IntInput(name='Select Value', value=5, step=1, start=1, end=9)

star_location= int_input.value

marker= p.star(star_location, 1, size=20, color="navy", alpha=0.5, legend_label="You are here")

def plot_update(event):
    
    r = Range1d
   
    r.star_location= int_input.value
    
    marker.update(star_location)

int_input.param.watch(plot_update, 'value')


pn.Column(int_input, p)

I also tried using renderers.remove(), but I would rather update if possible.

x = list(range(1,10))
y = list(1 for i in range(1,10)) 

p = figure(
    height=130,
    width=1800)
p.line(x=x, y=y)

int_input = pn.widgets.IntInput(name='Select Value', value=5, step=1, start=1, end=9)

star_location= int_input.value

marker= p.star(star_location, 1, size=20, color="navy", alpha=0.5, legend_label="You are here")

def plot_update(marker):
       
    p.renderers.remove(marker)
   
    star_location= int_input.value
    
    marker= p.star(star_location, 1, size=20, color="navy", alpha=0.5, legend_label="You are here")

int_input.param.watch(plot_update, 'value')


pn.Column(int_input, p)

If the glyph property is driven by a ColumnDataSource that you create, you can update the data in your CDS to update glyph drawing. This is probably the most common case, and there are lots of relevant examples in the docs and repo for that case.

But if you want to update a glyph property that is configured with a single value and not driven by a data source, you can do that by updating the relevant properties on the glyph:

from bokeh.plotting import figure, show

p = figure()

r = p.circle(x=10, y=20)

r.glyph.x = 20 # <---- 

show(p)

Edit: also please note that individual methods for scatter markers, e.g. p.star are about to be deprecated. Instead prefer the scatter method (added prior to 3.1.1) with a marker specified:

p.scatter(..., marker="star")

That works! Thank you Bryan!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.