Hello,
i have a code where i plot a large amount of polygons using the multi_polygons glyph. If i use the hover_fill_alpha parameter, things get laggy as the whole glyph gets redrawn everythime the mouse goes to another polygon, which is acceptable. I would like to be able to enable/disable this feature.
I first thought of having two glyphs, one with it, and one without the alpha.
The glyph without the alpha runs smooth on its own, but if it has a glyph with the alpha, even though it is hidden, things are laggy. Is there a way to disable those computation when a the glyph is hidden? How would you do it?
from bokeh.plotting import figure
from bokeh.models import (
HoverTool,
ColumnDataSource,
)
import panel as pn
def get_shapes():
shapes_list = []
colors_list = []
count_x = 230
for i in range(count_x):
for j in range(count_x):
shapes_list.append(([i, i + 1, i + 1, i], [j, j, j + 1, j + 1]))
colors_list.append(
(
int(255 * i / count_x),
int(255 * j / count_x),
int(255 * i / count_x),
255,
)
)
return shapes_list, colors_list
coords, colors = get_shapes()
xs = [c[0] for c in coords]
ys = [c[1] for c in coords]
edge_colors = [(c[0] * 0.9, c[1] * 0.9, c[2] * 0.9, c[3]) for c in colors]
TOOLTIPS = [
("Volume ID", "@volume_name"),
]
display_as_polygons = True
MyHover = HoverTool(tooltips=TOOLTIPS)
xs_dict = [[{"exterior": e, "holes": []}] for e in xs]
ys_dict = [[{"exterior": e, "holes": []}] for e in ys]
xs = [[[p["exterior"], *p["holes"]] for p in mp] for mp in xs_dict]
ys = [[[p["exterior"], *p["holes"]] for p in mp] for mp in ys_dict]
source_polygons = ColumnDataSource(
{
"xs": xs,
"ys": ys,
"color": colors,
"edge_color": edge_colors,
"volume_name": list(range(len(colors))),
}
)
# Create a new figure with specified tools
p = figure(
tools="pan,wheel_zoom,box_select,reset",
width_policy="max",
height_policy="max",
match_aspect=True,
)
p.add_tools(MyHover)
p.multi_polygons(
xs="xs",
ys="ys",
line_width=2,
color="color",
# hover_fill_alpha=.6,
# hover_fill_color="edge_color",
line_color="edge_color",
source=source_polygons,
)
p.multi_polygons(
xs="xs",
ys="ys",
line_width=2,
color="color",
hover_fill_alpha=.6,
# hover_fill_color="edge_color",
line_color="edge_color",
source=source_polygons,
visible=False
)
p.title.text = "Plot with polygons"
pn.pane.Bokeh(p, sizing_mode="stretch_both").show()
Thanks,
Thibault