In this article, we will be learning about how to change the size of labels in bokeh legend. Legend plays a very important role in bokeh. With the help of legend, we can point to various types of glyphs in a plot that bokeh provides us, and can customize them according to our choice. Along with that, we can also change various properties of the glyphs text shown in a box in a plot. Apart from that, there are various attributes that we can use to customize a legend.
Some of the attributes that can be used to customize a glyph: legend.label_text_font, legend.label_text_font_size, legend.location, legend.title, legend.orientation and, legend.clicking_policy.
Now, before proceeding further, we must be sure to have bokeh installed on our local device. If not, then open a command prompt and type the following command:
pip install bokeh
Now we are ready to go. It is necessary to install bokeh, else the functionalities won’t work. So, let us move to the implementation of the above concept.
In the following example, we will be changing the font size of the labels using bokeh legend and along with that, we will be learning some other features too.
In the above code, after initializing all the necessary packages from the libraries and modules, we are creating two points x and y which are a list of arbitrary values. Now we will be plotting those values in our graph. So, for that, we are creating an empty figure/plot whose plot width and height are given as 600. After that, we are plotting the two graphs where points on the first graph are in the shape of a circle and the points on the second graph are in the shape of a square. Apart from the different colors of the two graphs, in order to differentiate between them, we are creating a box where we are providing the labels for both the glyphs using legend_label and we are also giving a title to that box. Now comes the main implementation i.e changing the size of the labels using bokeh legend. So, by using legend.label_text_font_size we are customizing the size of the labels and glyphs in the box, and since we have given a title to the box, legend.label_text_font_size helps us to change the size of the title. Along with changing the size, we are also changing its orientation i.e on the top-left corner using legend.location.
Now, in the second example, lets us explore some more attributes of how to change sizes using legend. In the example below, instead of using label_text_font_size, we can also use glyph_height, glyph_width, label_height, label_width which are responsible for changing the labels and the glyph sizes separately in the label box differentiating all the lines. Let us move to the main implementation now.
In the above code, we have imported numpy package along with the figure and show for plot creation and showing. After importing the packages, we are creating three different lists of numbers which are then plotted against each other in graphical format. They are of different shapes. Using legend in bokeh, we are labeling them differently, and using the following attributes mentioned above, we are changing the size of the glyphs and the labels separately which also affects the box size.
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