Removing / Redrawing annotations

I am using the following to add annotations to my plot:

Add arrow

plot.add_layout(Arrow(end=VeeHead(line_color=“grey”, line_width=2, size=3),
x_start=0, y_start=0, x_end=newX, y_end=newY))

Add label

plot.add_layout(Label(x=newX, y=newY, text=columns[i], render_mode=‘canvas’,
border_line_color=‘black’, border_line_alpha=1.0,
background_fill_color=‘white’, background_fill_alpha=1.0, text_font_size=“6pt”))

However, when I update the data source and the plot, these annotations stay in place. How do I remove them, so that I can redraw them?

I am not sure how to remove annotations but if you save a reference to it you can change its attributes instead of redrawing. E.g.

label = Label(…)

plot.add_layout(label)

later in a callback

label.x = …

···

On Tuesday, September 20, 2016 at 6:51:02 AM UTC-4, Mathias Gruber wrote:

I am using the following to add annotations to my plot:

Add arrow

plot.add_layout(Arrow(end=VeeHead(line_color=“grey”, line_width=2, size=3),
x_start=0, y_start=0, x_end=newX, y_end=newY))

Add label

plot.add_layout(Label(x=newX, y=newY, text=columns[i], render_mode=‘canvas’,
border_line_color=‘black’, border_line_alpha=1.0,
background_fill_color=‘white’, background_fill_alpha=1.0, text_font_size=“6pt”))

However, when I update the data source and the plot, these annotations stay in place. How do I remove them, so that I can redraw them?

Hi Mathias,

I had the same question recently, here is a minimal example using two methods.

  1. updating the annotation parameters in the server side

from bokeh.io import curdoc

from bokeh.layouts import row, widgetbox

from bokeh.models import Slider, Span, Label

from bokeh.plotting import figure

slider = Slider(start=0, end=10, value=3, step=0.1, title=‘Slider’)

plot = figure(width=700, height=250, x_range=(0,10), y_range=(-1, 1))

span = Span(location=slider.value, dimension=‘height’)

plot.add_layout(span)

label = Label(x=slider.value, y=0, x_units=‘data’, y_units=‘data’,

             text="Minimum")

plot.add_layout(label)

def update_annotations(attr, old, new):

span.location = new
label.x = new

slider.on_change(‘value’, update_annotations)

curdoc().add_root(row(plot, widgetbox(slider)))

  1. Using a JS callback to update the annotation parameters

from bokeh.io import curdoc

from bokeh.layouts import row, widgetbox

from bokeh.models import Slider, Span, CustomJS, Label

from bokeh.plotting import figure

slider = Slider(start=0, end=10, value=3, step=0.1, title=‘Slider’)

plot = figure(width=700, height=250, x_range=(0,10), y_range=(-1, 1))

span = Span(location=slider.value, dimension=‘height’)

plot.add_layout(span)

label = Label(x=slider.value, y=0, x_units=‘data’, y_units=‘data’,

             text="Minimum")

plot.add_layout(label)

slider.callback = CustomJS(args=dict(span=span, label=label, slider=slider), code="""

span.location = slider.value
label.x = slider.value

“”")

curdoc().add_root(row(plot, widgetbox(slider)))


<details class='elided'>
<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>

On Tue, Sep 20, 2016 at 5:20 PM, Henry Harrison <[email protected]> wrote:
> I am not sure how to remove annotations but if you save a reference to it you can change its attributes instead of redrawing. E.g.

> 

> label = Label(...)

> plot.add_layout(label)

> 

> # later in a callback

> label.x = ...

> 
> 
> On Tuesday, September 20, 2016 at 6:51:02 AM UTC-4, Mathias Gruber wrote:
> > I am using the following to add annotations to my plot:
> > 

> > > # Add arrow
> > > plot.add_layout(Arrow(end=VeeHead(line_color="grey", line_width=2, size=3), 
> > >                                                   x_start=0, y_start=0, x_end=newX, y_end=newY))
> > >                 
> > > # Add label
> > > plot.add_layout(Label(x=newX, y=newY, text=columns[i], render_mode='canvas',
> > >                  border_line_color='black', border_line_alpha=1.0,
> > >                  background_fill_color='white', background_fill_alpha=1.0, text_font_size="6pt"))
> > 

> > However, when I update the data source and the plot, these annotations stay in place. How do I remove them, so that I can redraw them?

> --
> 
> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> 
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> 
> To post to this group, send email to [email protected].
> 
> To view this discussion on the web visit [https://groups.google.com/a/continuum.io/d/msgid/bokeh/ea8383a7-7ee0-4f97-b4e8-8b16f63a7d9e%40continuum.io](https://groups.google.com/a/continuum.io/d/msgid/bokeh/ea8383a7-7ee0-4f97-b4e8-8b16f63a7d9e%40continuum.io?utm_medium=email&utm_source=footer).
> 
> 
> For more options, visit [https://groups.google.com/a/continuum.io/d/optout](https://groups.google.com/a/continuum.io/d/optout).

Angelo Fausti Neto
LIneA/LSST

</details>

Excellent, works perfectly, thanks

···

On Wed, Sep 21, 2016 at 2:50 AM, Angelo Fausti Neto [email protected] wrote:

Hi Mathias,

I had the same question recently, here is a minimal example using two methods.

  1. updating the annotation parameters in the server side


from bokeh.io import curdoc

from bokeh.layouts import row, widgetbox

from bokeh.models import Slider, Span, Label

from bokeh.plotting import figure

slider = Slider(start=0, end=10, value=3, step=0.1, title=‘Slider’)

plot = figure(width=700, height=250, x_range=(0,10), y_range=(-1, 1))

span = Span(location=slider.value, dimension=‘height’)

plot.add_layout(span)

label = Label(x=slider.value, y=0, x_units=‘data’, y_units=‘data’,

             text="Minimum")

plot.add_layout(label)

def update_annotations(attr, old, new):

span.location = new
label.x = new

slider.on_change(‘value’, update_annotations)

curdoc().add_root(row(plot, widgetbox(slider)))

  1. Using a JS callback to update the annotation parameters

from bokeh.io import curdoc

from bokeh.layouts import row, widgetbox

from bokeh.models import Slider, Span, CustomJS, Label

from bokeh.plotting import figure

slider = Slider(start=0, end=10, value=3, step=0.1, title=‘Slider’)

plot = figure(width=700, height=250, x_range=(0,10), y_range=(-1, 1))

span = Span(location=slider.value, dimension=‘height’)

plot.add_layout(span)

label = Label(x=slider.value, y=0, x_units=‘data’, y_units=‘data’,

             text="Minimum")

plot.add_layout(label)

slider.callback = CustomJS(args=dict(span=span, label=label, slider=slider), code=“”"

span.location = slider.value
label.x = slider.value

“”")

curdoc().add_root(row(plot, widgetbox(slider)))

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/CAD%3DOK5PxL15WkvXGhnGaqX53E5Ji9Xu2ciOq-Av78LMWco6UoA%40mail.gmail.com.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Angelo Fausti Neto
LIneA/LSST

On Tue, Sep 20, 2016 at 5:20 PM, Henry Harrison [email protected] wrote:

I am not sure how to remove annotations but if you save a reference to it you can change its attributes instead of redrawing. E.g.

label = Label(…)

plot.add_layout(label)

later in a callback

label.x = …

On Tuesday, September 20, 2016 at 6:51:02 AM UTC-4, Mathias Gruber wrote:

I am using the following to add annotations to my plot:

Add arrow

plot.add_layout(Arrow(end=VeeHead(line_color=“grey”, line_width=2, size=3),
x_start=0, y_start=0, x_end=newX, y_end=newY))

Add label

plot.add_layout(Label(x=newX, y=newY, text=columns[i], render_mode=‘canvas’,
border_line_color=‘black’, border_line_alpha=1.0,
background_fill_color=‘white’, background_fill_alpha=1.0, text_font_size=“6pt”))

However, when I update the data source and the plot, these annotations stay in place. How do I remove them, so that I can redraw them?

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/ea8383a7-7ee0-4f97-b4e8-8b16f63a7d9e%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Best Regards,

Mathias Gruber