Here is an example snippet to show the issue.
Steps:
- run bokeh server with this code
- press the “get data” button
- press the " get data" button again
- click on the legends to hide the lines.
If the are lines still showing from the first plotting, then these lines will not be removed hidden from the plot!
import pandas as pd
import numpy as np
from bokeh.models import ColumnDataSource, Button, Legend
from bokeh.plotting import figure
from bokeh.palettes import d3
from bokeh.layouts import layout
from bokeh.io import curdoc
# Global variables
df = None
fig = None
# defs
def get_data():
global df
df = pd.DataFrame({'p1' : np.random.randint(0, 10, 31),
'p2' : np.random.randint(0, 10, 31),
'p3' : np.random.randint(0, 10, 31)},
index=pd.date_range("2022-01-01", periods=31, freq="D"))
df.index.name = "Date"
# bokeh specific
button = Button(label="Get data")
source = ColumnDataSource({})
tools = ["box_select","pan","wheel_zoom","box_zoom","reset","save","hover"]
tooltips = [
('value', '$y'),
('title', '$name')
]
fig = figure(plot_width=1000, plot_height=400,
title = "signals",
x_axis_type='datetime',
y_axis_label='Signal value',
tools=tools, tooltips=tooltips)
fig.add_layout(Legend(), 'right')
fig.legend.click_policy= 'hide'
def update_fig():
global fig, df, source
get_data()
source.data = df
cols = df.columns
dict = {}
for signalData, cc, title in zip(cols, d3['Category10'][len(cols)], cols):
dict[title] = fig.line('Date', signalData, source=source, color=cc, name=title)
legends = [(tt, [dict[tt]]) for tt in dict]
fig.legend.update(items = legends)
def button_callback():
global fig, source
source.data = {}
fig.legend.items = []
update_fig()
button.on_click(button_callback)
layout = layout([[fig], [button]])
curdoc().add_root(layout)
curdoc().title = "snippet plot clear issue"