Unable to hide legends with glyphs

I wrote the following code to hide the glyphs created in the plot:

valueline_model_to_hide = curdoc().get_model_by_name(‘decreasing value line candle’)
valueline_model_to_hide.visible = False

But the problem is I’m not able to hide the legends of the corresponding glyphs with this. How shall I hide both the glyphs and the legends for that glyphs?

@Umang_Garg

Not sure if it fits your use case, but you can do this interactively with the legend click-policy.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
"""

from bokeh.plotting import figure
from bokeh.io import curdoc

# model names
glyph_name = 'glyph'
legend_label = 'Curve'


p = figure(width=400, height=300)
_ = p.line(x=[0.0,0.5,1.0], y=[0.0,1.0,0.0], color='#000000', name=glyph_name+'_a', legend_label=legend_label+' A')
_ = p.line(x=[0.0,0.5,1.0], y=[1.0,0.0,1.0], color='#ff0000', name=glyph_name+'_b', legend_label=legend_label+' B')

p.legend.click_policy = 'hide'

curdoc().add_root(p)

I think @Umang_Garg wants to actually wants the converse, i.e. to remove the row from the legend, when the glyph is hidden programmatically. Unfortunately there is not any simple way to accomplish this. The only way that might work is to actually root through the Legend object and remove the LegendItem for the row you don’t what to display. And, of course, manually add it back if you want to see it again.

It would probably make sense for LegendItem to gain a visible property. This would make a “Good First Issue” for the issue tracker, so I encourage you to make a feature request.

Hi Bryan! can you tell me the command to remove the LegendItem?

@Umang_Garg There’s not any specific command. Plot.legend[0].items is a list of LegendItem objects. You will have to inspect them and remove the one you don’t want there.

Command to remove?