- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Labels in Bokeh
Bokeh is a Python interactive visualisation package that offers a wide range of features beyond merely plot creation. Labelling is one of the useful characteristics that it has. With the help of this tool, developers may give the plot elements verbal descriptions, which makes the data easier to understand. In order to help readers better grasp how labels are used in bokeh, this article delves further into the topic.
Introduction to Bokeh
Understanding Bokeh's goal is crucial before we go into labels. Bokeh makes it easier to create intricate statistical plots and assists in turning data into visually appealing, interactive, and understandable graphs. It presents huge or streaming datasets with high-performance interaction and is designed for current web browsers.
The Importance of Labels in Bokeh
Any data visualisation needs labels because they provide the data being shown context and meaning. Without labels, a visualisation could not be clear or perhaps seem pointless. Bokeh offers numerous methods for adding and customising labels, enabling developers to produce interesting and thorough visualisations.
Types of Labels in Bokeh
In Bokeh, labels come in a variety of forms, including but not restricted to −
Title − The plot's title, which is typically at the top, provides a brief summary of the plot's goal or the data it depicts.
Axis Labels − The variables shown on the x and y axes are identified by the axis labels.
Legend − A legend aids in identifying several categories or groups within the data.
Data Labels − The graph's data points may be given textual cues or labels known as data labels.
Annotations − These are additional pieces of text or marks that give the data more context.
Implementing Labels in Bokeh: A Practical Example
Now that we have an understanding of how to use labels in Bokeh, let's look at some instances.
from bokeh.plotting import figure, show, output_notebook from bokeh.models import Label # Initialize output to notebook output_notebook() # Create a new plot with a title and axis labels p = figure(title="Bokeh Label Example", x_axis_label='x', y_axis_label='y') # Add a line to the plot p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2) # Add a label to the plot label = Label(x=3, y=4, text="Data Point (3,4)") p.add_layout(label) # Show the plot show(p)
In the aforementioned illustration, a label identifying a particular data point at (3,4) has been added to the plot. This label can be added using the Label class from the bokeh.models module.
Adding Multiple Labels in Bokeh
Bokeh allows for the addition of numerous labels to a plot, as demonstrated in the example below:
from bokeh.plotting import figure, show, output_notebook from bokeh.models import LabelSet, ColumnDataSource # Initialize output to notebook output_notebook() # Create a new plot with a title and axis labels p = figure(title="Multiple Labels Example", x_axis_label='x', y_axis_label='y') # Prepare some data data = {'x_values': [1, 2, 3, 4, 5], 'y_values': [6, 7, 2, 4, 5], 'labels': ['Point 1', 'Point 2', 'Point 3', 'Point 4', 'Point 5']} # Create a ColumnDataSource object source = ColumnDataSource(data=data) # Add glyphs (circles) to the plot p.circle('x_values', 'y_values', size=20, source=source) # Create a LabelSet labels = LabelSet(x='x_values', y='y_values', text='labels', level='glyph', x_offset=5, y_offset=5, source=source, render_mode='canvas') # Add the labels to the plot p.add_layout(labels) # Show the plot show(p)
In this illustration, we make many labels for various points. In a data dictionary, we first specify the points and their accompanying labels. Then, using this information, we build a ColumnDataSource that acts as a conduit between our Python data and the Bokeh graphic.
The LabelSet class, which enables us to define a set of labels with their placements and texts, is used to build the labels. The plot is then updated with these labels using the add_layout method.
Customizing Labels in Bokeh
Labels can also be altered in a number of ways with Bokeh, including their colour, text, and angle. An illustration of how you could apply these customizations is given below:
from bokeh.plotting import figure, show, output_notebook from bokeh.models import Label # Initialize output to notebook output_notebook() # Create a new plot with a title and axis labels p = figure(title="Custom Label Example", x_axis_label='x', y_axis_label='y') # Add a line to the plot p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2) # Add a custom label to the plot label = Label(x=3, y=4, text="Data Point (3,4)", text_color="red", text_font_size="10pt", angle=30, background_fill_color="blue", background_fill_alpha=0.1) p.add_layout(label) # Show the plot show(p)
In this illustration, the label is personalised with a red font colour, a particular text size, a rotation angle, and a blue background fill that is partially translucent.
Conclusion
You may produce more useful and perceptive data visualisations by comprehending and utilising labels in Bokeh. Labels give the required context to make your visualisations thorough and illuminating, whether you're just giving your plot a title or assigning particular labels to certain data points. As we've seen, Bokeh's features give you a versatile and potent toolkit for annotating your Python plots.