Create a two line legend in a Bokeh Plot
In this article, we will be learning about how to create a two line legend in the bokeh plot. Legends are one of the most essential parts of a bokeh plot. They help us to differentiate between different glyphs used in a plot. Other than that if we need to plot different lines of different colors in the same plot, we can differentiate them using legend. Also, we can change various properties of the things mentioned in the legend.
But before we dive deep into the topic, we should either have google colab or should be using a local device code editor. If we are working in google colab, then we can straight away go to the below implementation. But if we are using a local device, then we should ensure that we have bokeh preinstalled in our device, else we can install it too. Open a command prompt and write
pip install bokeh
Now, everything is set. The installation is essential in our local device else the functionalities won’t work. Let’s move to the above implementation.
In the first example, we are looking at some lines which are plotted using a wide range of points in an empty canvas. Using legend in bokeh we are differentiating the lines. The main thing to notice here is, we are having a two-line legend. So without further wait, let’s go to the code and see how it works.
- In the above example, we are importing all the necessary packages at first such as show, figure, Legend, and numpy from different modules of the bokeh library that python provides us.
- After importing the packages, we are creating an empty figure of plot width and height as 600.
- After that, we are creating four different lines with a different set of points along with different line colors and line width. In order to identify them, a legend is used.
- Now to create a two-line legend, we are using two variables legend1 and legend2 where we are storing two points in each of the variables where we are providing the orientation as horizontal. As a result, two will be shown in each row.
- Using ‘add_layout‘ we are determining the position of the legend as above i.e it will be shown at the top of the graphical representation.
- Finally, we are showing the plot using a show from bokeh.io.
In the next example, we will be exploring the concept with some glyphs provided by bokeh. Bokeh provides us with various types of glyphs, so we will be implementing the same concept used in the earlier example.
- After importing the required packages, we are taking two variables x1 and y1 with a set of points that are to be plotted later.
- After that, we are setting the plot width and plot height as 600. Now, bokeh provides us with a variety of glyphs that can be used to differentiate different plots in a graph. So, we took some of the glyphs to point 6 such graphs in our plot with a wide range of points.
- After pointing them, we used the legend to identify each of the glyphs and we kept two points beside each other along with their orientation as ‘horizontal‘.
- Now using ‘add_layout‘ option, we can determine the position of the legend box. In this case, we are placing it at the bottom.
- Finally, we are showing the figure with the help of ‘show‘ from ‘bokeh.io‘.
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course