Skip to content
Related Articles

Related Articles

Python Bokeh – Making Interactive Legends

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Jul, 2020

Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity.

How to make Interactive legends?

The legend of a graph reflects the data displayed in the graph’s Y-axis. In Bokeh, the legends correspond to glyphs. There are two ways to make legends interactive: 

  • Hiding
  • Muting

Hiding Glyphs

A Glyph can be hidden from the legend by setting the legend click_policy property to “hide”.

Example :

Python3




# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# file to save the model 
output_file("gfg.html"
         
# instantiating the figure object 
graph = figure(title = "Bokeh Hiding Glyphs"
  
# plotting the graph
graph.vbar(x = 1,  top = 5,
           width = 1, color = "violet",
           legend_label = "Violet Bar")
graph.vbar(x = 2,  top = 5,
           width = 1, color = "green",
           legend_label = "Green Bar")
graph.vbar(x = 3,  top = 5,
           width = 1, color = "yellow",
           legend_label = "Yellow Bar")
graph.vbar(x = 4,  top = 5,
           width = 1, color = "red",
           legend_label = "Red Bar")
  
# enable hiding of the glyphs
graph.legend.click_policy = "hide"
  
# displaying the model 
show(graph) 

Output : 

Muting Glyphs

Hiding the glyph makes it vanish completely, on the other hand, muting the glyph just de-emphasizes the glyph based on the parameters. A Glyph can be muted from the legend by setting the legend click_policy property to “mute”.
 

Python3




# importing the modules 
from bokeh.plotting import figure, output_file, show 
  
# file to save the model 
output_file("gfg.html"
         
# instantiating the figure object 
graph = figure(title = "Bokeh Hiding Glyphs"
  
# plotting the graph
graph.vbar(x = 1,  top = 5,
           width = 1, color = "violet",
           legend_label = "Violet Bar",
           muted_alpha=0.2)
graph.vbar(x = 2,  top = 5,
           width = 1, color = "green",
           legend_label = "Green Bar",
           muted_alpha=0.2)
graph.vbar(x = 3,  top = 5,
           width = 1, color = "yellow",
           legend_label = "Yellow Bar",
           muted_alpha=0.2)
graph.vbar(x = 4,  top = 5,
           width = 1, color = "red",
           legend_label = "Red Bar",
           muted_alpha=0.2)
  
# enable hiding of the glyphs
graph.legend.click_policy = "mute"
  
# displaying the model 
show(graph) 

Output : 
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!