Hi, sorry for a noob question, but I have not been able to find a clean way of deleting annotations. In the example below, there are two methods, the currently uncommented method works, but is kind of hokey… first looping through the renderers to get the ones you want to delete and then looping through again to delete them. The other commented out method doesn’t work maybe due to internal array indexing issues or something. Anyway, is there a cleaner / better way of deleting a list of annotations from a plot? I have searched and haven’t been able to find anything.
“”"
example derived from:
https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html#box-annotations
example designed to work on bokeh server.
“”"
from bokeh.plotting import figure, curdoc
from bokeh.layouts import row
from bokeh.plotting import show, output_file
from bokeh.models import BoxAnnotation
TOOLS = “pan,wheel_zoom,box_zoom,reset,save”
x = [0,1,2,3]
y = [-10,220,40,90]
p = figure(x_axis_type=“datetime”, tools=TOOLS)
p.line(x=x, y=y, line_color=“gray”, line_width=1)
low_box = BoxAnnotation(top=80, fill_alpha=0.1, fill_color=‘red’)
mid_box1 = BoxAnnotation(name=‘mid_box’,bottom=80, top=90, fill_alpha=0.1, fill_color=‘green’)
mid_box2 = BoxAnnotation(name=‘mid_box’,bottom=100, top=100, fill_alpha=0.1, fill_color=‘green’)
mid_box3 = BoxAnnotation(name=‘mid_box’,bottom=110, top=120, fill_alpha=0.1, fill_color=‘green’)
mid_box4 = BoxAnnotation(name=‘mid_box’,bottom=120, top=130, fill_alpha=0.1, fill_color=‘green’)
mid_box5 = BoxAnnotation(name=‘mid_box’,bottom=130, top=140, fill_alpha=0.1, fill_color=‘green’)
mid_box6 = BoxAnnotation(name=‘mid_box’,bottom=140, top=150, fill_alpha=0.1, fill_color=‘green’)
mid_box7 = BoxAnnotation(name=‘mid_box’,bottom=150, top=160, fill_alpha=0.1, fill_color=‘green’)
mid_box8 = BoxAnnotation(name=‘mid_box’,bottom=160, top=170, fill_alpha=0.1, fill_color=‘green’)
mid_box9 = BoxAnnotation(name=‘mid_box’,bottom=170, top=180, fill_alpha=0.1, fill_color=‘green’)
high_box = BoxAnnotation(bottom=180, fill_alpha=0.1, fill_color=‘red’)
p.add_layout(low_box)
p.add_layout(mid_box1)
p.add_layout(mid_box2)
p.add_layout(mid_box3)
p.add_layout(mid_box4)
p.add_layout(mid_box5)
p.add_layout(mid_box6)
p.add_layout(mid_box7)
p.add_layout(mid_box8)
p.add_layout(mid_box9)
p.add_layout(high_box)
p.title.text = “Glucose Range”
p.xgrid[0].grid_line_color=None
p.ygrid[0].grid_line_alpha=0.5
p.xaxis.axis_label = ‘Time’
p.yaxis.axis_label = ‘Value’
“”"
“”"
In[0]:
···
#=============================================================================
This works
remove_these =
j = 0
identify the items to delete
for i,r in enumerate(p.renderers):
print(“renderer loop identification phase” + str(type(r)))
if r.name == ‘mid_box’:
j = j+1
remove_these.append(r)
delete them
for j,r in enumerate(remove_these):
print(“renderer loop deleting phase” + str(type(r)))
p.renderers.remove(remove_these[j])
“”"
“”"
In[1]:
#=============================================================================
This doesn’t work
“”"
print(">>> oops, I changed my mind… let’s delete that middle box annotation.")
for i,r in enumerate(p.renderers):
print(“renderer loop” + str(type(r)))
if r.name == ‘mid_box’:
p.renderers.remove(r)
print("<<<dang… didn’t work for all of them")
“”"
In[2]:
layout = row(p)
curdoc().add_root(layout)
curdoc().title = ‘Layout Buttons’