Create a plot with Multiple Glyphs using Python Bokeh
In this article, we will be learning about multiple glyphs and also about adding a legend in bokeh. Now bokeh provides us with a variety of glyphs that can be used to represent a point in a plot. Some glyphs are circle, square, asterik, inverted_triangle(), triangle() etc.
This module does not come in-built with Python. To install it type the below command in the terminal.
pip install bokeh
Multiple Glyphs in Bokeh
In this example, we will be exploring different types of glyphs in bokeh. Every glyph in the below implementation has different properties to identify themselves. So, lets us move to the code to understand the concept.
- At first, we are importing numpy, figure, and show packages from different modules.
- Now, we are creating a range of numbers using arrange and storing the list in x.
- Thereafter, we are also initializing different variables in order to plot the graph against each other.
- After initialization, we are creating an empty figure with plot width and height as 600.
- Since bokeh provides us with different types of glyphs, we are implementing some of them in this example with various colors and sizes of the points. The set of points with shape as circle has a color red and size as 20 whereas the set of points with shape as a hex has green color along with a size of 10 and the opacity of the color is 0.7(denoted by alpha).
Multiple glyphs with bokeh legend
Now, lets us move to the next topic of this article and that is bokeh legend. Legend plays a very important role in bokeh plots. They help us to identify different types of glyphs that are used in a plot. Along with that, we can also change different properties of the box that can differentiate different glyphs.
In this example, we will be exploring multiple glyphs along with bokeh legend to identify them and also changing some properties of the box differentiating the plots. This is important because, in a plot with various types of glyphs, we need something to identify them and that is when bokeh legend plays its role. Lets us move to the code implementation now.
- We are creating three different variables with a set of values that are plotted against each other.
- We can see three plots, each of different glyphs. According to the code we have provided circular glyphs with size 30, square glyphs with size 20, and inverted triangle glyphs with size 15. Also, all the glyphs are of different colors too.
The main concept that lies in this example is, we have provided labels for each of the plots in the graph, and using legend in bokeh, we are changing the properties of the glyphs shown in the box in the top right corner(which is by default).
In the last example, we will be importing a dataset to our code. After that we will be plotting different graphs and using bokeh legend, we will be changing the label size and glyph size respectively. Let us move to the implementation.
- Along with importing all the packages which we have imported earlier, this time we are importing a new dataset auto-mpg. Along with that, we are also importing the ColumnDataSource package.
- With ColumnDataSouce package we are creating it manually which helps us to share data between multiple plots and widgets.
- After that, we are plotting data taken from auto-mpg data-set against each other.
- Then using legend property of bokeh (figure.legend.label_width,figure.legend.glyph_width,figure.legend.label_height,figure.legend.glyph_height) we are changing the size of the labels and glyphs used to identify plots in the graph.
Please Login to comment...