Labels in Bokeh

20 Sep, 2021
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
# importing show from bokeh.io# module to show the plotfrom bokeh.io import show # importing figure from# bokeh.plotting to create an# empty plotfrom bokeh.plotting import figure  # Creating a set of points using# two arrays x and yx = [1, 2, 3, 4, 5]y = [2, 3, 5, 4, 6] # Creating an empty figure with plot width# and height as 600p = figure(plot_width=600, plot_height=600) # defining that the points should be joined# with a linep.line(x, y) # Defining the X-Axis Labelp.xaxis.axis_label = "X-Axis Label" # defining the Y-Axis Labelp.yaxis.axis_label = "Y-Axis Label" # showing the above plotshow(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
# importing show from bokeh.io# module to show the plot# importing show from bokeh.iofrom bokeh.io import show # importing label from# bokeh.models.annotations modulefrom bokeh.models.annotations import Label # importing figure from bokeh.plotting# modulefrom bokeh.plotting import figure  # Creating an empty figure with plot# width and size to be 600 and 500# respectivelyp = figure(plot_width=600, plot_height=500) # Plotting the points in the shape of# a circle with color green and size 10p.circle([2, 5, 8], [4, 7, 6], color="green", size=10) # Labelling the X-Axisp.xaxis.axis_label = "X-Axis-Label" # Labelling the Y-Axisp.yaxis.axis_label = "Y-Axis_Label" # Creating a label for the point (5,7)# where the text for the point will be# "Second Point" alng with defining# the position of the textlabel = Label(x=5, y=7, x_offset=10, y_offset=-30, text="Second Point") # Implementing label in our plotp.add_layout(label) # Showing the above plotshow(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
# importing figure from# Bokeh.plottingfrom bokeh.plotting import figure # importing ColumnDataSource and LabelSet# from bokeh.modelsfrom bokeh.models import ColumnDataSource, LabelSet # import show from broken.io modulefrom bokeh.io import show  # Using ColumnDataSource we are providing# Column names to a sequence of valuessource = 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'])) # Creating an empty figurep = figure(plot_width=750, plot_height=600) # Plotting the data in the form of trianglep.triangle(x='roll_no', y='marks', size=8, source=source) # Labelling the X-Axisp.xaxis.axis_label = 'Roll_Numbers' # Labelling the Y-Axisp.yaxis.axis_label = 'Marks' # Using LabelSet, we are labelling each of the# points with names created in sourcelabels = LabelSet(x='roll_no', y='marks', text='names',                  x_offset=5, y_offset=5, source=source) Adding that label to our figurep.add_layout(labels) # Showing the above plotshow(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 packageimport numpy as np # importing figure and show from# bokeh.plottingfrom bokeh.plotting import figure, show  # Creating an array of 100 numbers# between 0 to 1 using linespacex = np.linspace(0, 1, 100) # Creating an array of sin# values of x in yy = np.sin(x) # Creating an empty figurep = figure(plot_width=500, plot_height=500) # Creating the first plot with color as red and label as# sin(x)p.circle(x, y, legend_label="1st Plot", color="red") # Drawing a line through all the points with the# same colorp.line(x, y, legend_label="1st Plot", line_color="red") # Drawing the second plot as yellow and labelling as# the 2nd plotp.circle(x, y**3, legend_label="2nd Plot", color="yellow") # Drawing a line through all the points of the# second plotp.line(x, y**3, legend_label="2nd Plot", line_color="yellow", line_width=2) # showing the above plotshow(p)
Output:
kalrap615
Picked
Python-Bokeh
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
Introduction To PYTHON
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list