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.
Code:
Python3
# importing numpy package import numpy as np # importing figure and show from # bokeh.plotting module from bokeh.plotting import figure, show # using numpy package to # create a list of 5 numbers x = np.arange( 5 ) # y is stored with the square # of the numbers in x y = x * * 2 # z is storing 3 times the value # of the elements in x z = x * 3 # storing 7 numbers in p between # 1 to 20 p = np.linspace( 1 , 20 , 7 ) # storing 7 numbers in q between # 1 to 10 q = np.linspace( 1 , 10 , 7 ) # storing 5 numbers in r between # 1 to 30 r = np.linspace( 1 , 30 , 5 ) # creating 31 elements in a list a = np.arange( 31 ) # creating an empty figure with specific plot # width and height fig = figure(plot_width = 600 , plot_height = 600 ) # plotting the points in the form of # circular glyphs fig.circle(x, y, color = "red" , size = 20 ) # plotting the points in the form of # square glyphs fig.square(x, z, color = "blue" , size = 15 , alpha = 0.5 ) # plotting the points in the form of # hex glyphs fig. hex (y, z, color = "green" , size = 10 , alpha = 0.7 ) # drawing a line between the plotted points fig.line(x, y, color = "green" , line_width = 4 ) # plotting the points in the form of # inverted triangle glyph fig.inverted_triangle(p, q, color = "yellow" , size = 20 , alpha = 0.4 ) # plotting the points in the form of # diamond glyphs fig.diamond(x, r, color = "purple" , size = 16 , alpha = 0.8 ) # plotting the points in the form of # cross glyphs fig.cross(a, a, size = 14 ) # showing the above plot show(fig) |
Output:
Explanation:
- 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).
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.
Installation
This module does not come in-built with Python. To install it type the below command in the terminal.
pip install bokeh