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.
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.
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:
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.