In this article, we will be learning about Labels in Bokeh. Labels are the short words or phrases that are used in order to define something in a plot.
Let us take an example that we are plotting a set of points in a graph. Now, if we’re dealing with real-life statistical data, then to plot the points in the graph, we need to label the X-Axis and Y-Axis with appropriate information in order to define what we are plotting against each other. That is where the term comes into place. They help us to determine what exactly we are plotting against in X and Y-Axis. Along with that, labels have various other functionalities in a plot.
So, let us take a simple implementation of having labels on X-Axis and Y-Axis with a group of simple points. Google Colab or any other text editor can be used for implementing the above concept. But if we are using our local device, be sure to have installed python earlier. And after that, we need to install bokeh. Open the command prompt and write the following code to install :
pip install bokeh
Now, let us take a simple example to have a little idea about labels in bokeh. Here we are taking a set of points and plotting them against each other with the X-Axis and Y-Axis having labels coded using bokeh.
Sample Code:
Python3
from bokeh.io import show
from bokeh.plotting import figure
x = [ 1 , 2 , 3 , 4 , 5 ]
y = [ 2 , 3 , 5 , 4 , 6 ]
p = figure(plot_width = 600 , plot_height = 600 )
p.line(x, y)
p.xaxis.axis_label = "X-Axis Label"
p.yaxis.axis_label = "Y-Axis Label"
show(p)
|
Output:
Now, let us dive a little deep into the topic. Apart from adding labels only in the axes in the above approach, bokeh.models.annotations provide us with a package called Labels. Labels come with various features which we are going to explore below:
In the code below, we are using labels from bokeh.models.annotations module and we are plotting a set of points on the graph. After labeling the X and the Y-Axis, we are labeling the second point in the plot. Along with that, we are also defining the position of the Label and ultimately showing the above plot.
Example:
Python3
from bokeh.io import show
from bokeh.models.annotations import Label
from bokeh.plotting import figure
p = figure(plot_width = 600 , plot_height = 500 )
p.circle([ 2 , 5 , 8 ], [ 4 , 7 , 6 ], color = "green" , size = 10 )
p.xaxis.axis_label = "X-Axis-Label"
p.yaxis.axis_label = "Y-Axis_Label"
label = Label(x = 5 , y = 7 , x_offset = 10 , y_offset = - 30 , text = "Second Point" )
p.add_layout(label)
show(p)
|
Output:
Let’s move to the next example:
In this example, we will be talking about another property of labels i.e LabelSet. But before going to the code, we should have a brief understanding of what is a LabelSet. Now, from the previous example, we can clearly see that with the help of labels, we are only able to label a single point in the graph i.e “Second Point”. But if we want to label all the points, then there is no point in repeating the code every time. So, in such a situation, bokeh has come up with a package known as LabelSet that helps us to label multiple points in the plot without repetition.In this example, we will be using another package i.e ColumnDataSource. ColumnDataSource provides us with the mapping of column names with a sequence of values.
Now, let’s move to the code to see the implementation:
Code:
Python3
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.io import show
source = ColumnDataSource(data = dict (
marks = [ 166 , 171 , 172 , 168 , 174 , 162 ],
roll_no = [ 0 , 1 , 2 , 3 , 4 , 5 ],
names = [ 'Joey' , 'Chandler' , 'Monica' ,
'Phoebe' , 'Rachel' , 'Ross' ]))
p = figure(plot_width = 750 , plot_height = 600 )
p.triangle(x = 'roll_no' , y = 'marks' , size = 8 , source = source)
p.xaxis.axis_label = 'Roll_Numbers'
p.yaxis.axis_label = 'Marks'
labels = LabelSet(x = 'roll_no' , y = 'marks' , text = 'names' ,
x_offset = 5 , y_offset = 5 , source = source)
Adding that label to our figure
p.add_layout(labels)
show(p)
|
Output:
Last but not the least, we will be learning another annotation i.e legend_label which helps us to differentiate between multiple plots in a graph. By using the tag “legend_label” we are actually defining the name of the plot which will be shown on the upper right side of the graph.
Code:
Python3
import numpy as np
from bokeh.plotting import figure, show
x = np.linspace( 0 , 1 , 100 )
y = np.sin(x)
p = figure(plot_width = 500 , plot_height = 500 )
p.circle(x, y, legend_label = "1st Plot" , color = "red" )
p.line(x, y, legend_label = "1st Plot" , line_color = "red" )
p.circle(x, y * * 3 , legend_label = "2nd Plot" , color = "yellow" )
p.line(x, y * * 3 , legend_label = "2nd Plot" , line_color = "yellow" , line_width = 2 )
show(p)
|
Output:
Last Updated :
20 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...