Open In App

Python Bokeh – Making Interactive Legends

Last Updated : 28 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 : 
 



Previous Article
Next Article

Similar Reads

Bokeh - Annotations and Legends
Prerequisites: Bokeh Bokeh includes several types of annotations to allow users to add supplemental information to their visualizations. Annotations are used to add notes or more information about a topic. Annotations can be titles, legends, Arrows, bands, labels etc. Adding legends to your figures can help to properly describe and define it. Hence
2 min read
Bokeh - Customising Legends
The legend of a graph reflects the data displayed in the graph’s Y-axis. In Bokeh, the legends correspond to glyphs. This article how Legends appearing in the bokeh plot can be customized. We can customize the legend with its several attributes such as location, color, line-color, font-size, font-style line width. We can modify it using the legend
3 min read
Interactive Data Visualization with Python and Bokeh
In this article, we'll learn how to do Interactive Data Visualization with Bokeh. Bokeh is a Python library that is used for creating interactive visualizations for modern web browsers. It handles custom or specialized use cases very simply. It provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums l
8 min read
Interactive visualization of data using Bokeh
Bokeh is a Python library for creating interactive data visualizations in a web browser. It offers human-readable and fast presentation of data in an visually pleasing manner. If you’ve worked with visualization in Python before, it’s likely that you have used matplotlib. But Bokeh differs from matplotlib. To install Bokeh type the below command in
4 min read
Add Interactive Slider to Bokeh Plots
Bokeh is an interactive Data visualization library of Python. It can be used to create interactive plots, dashboards, and data applications. Widgets are nothing but additional visual elements that you can add to your plots to interactively control your Bokeh document. There are various types of widgets such as button, div, spinner, slider, etc. In
2 min read
Interactive maps with Bokeh
Interactive maps are used to visualize the data based on the geo-location category. any large dataset which contains a lot of geo-location data like cities, states, countries, etc can be plotted easily. bokeh is an open-source package, which uses the Bokeh visualization tool. It gives a flexible declarative interface for dynamic web-based visualiza
4 min read
Python Bokeh - Making a Pie Chart
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. Let us see how to plot a pie chart in Bokeh. Does not provide us a module to directly plot a pie chart but we
3 min read
How to position legends inside a plot in Plotly-Python?
In this article, we will learn How to hide legend with Plotly Express and Plotly. A legend is an area describing the elements of the graph. In the plotly legend is used to Place a legend on the axes. Example 1: In this example, we are positioning legends inside a plot with the help of method fig.update_layout(), by passing the position as x=0.3 and
2 min read
How to set up multiple subplots with grouped legends using Plotly in Python?
Prerequisites: Python Plotly In this article, we will explore how to set up multiple subplots with grouped legends using Plotly in Python. It is a useful approach to demonstrate legend for a plot as it allows to reveal a large amount of information about complex information. A legend in the plotly library basically describes the graph elements. One
2 min read
How to change Seaborn legends font size, location and color?
Seaborn is a library for making statistical graphics on top of matplotlib with pandas data structures in python. Seaborn legend is the dialog box which is located on the graph which includes the description of the different attributes with their respected colors in the graph. We can easily change the properties of the seaborn legend including font
3 min read
Article Tags :
Practice Tags :