Bokeh - Plots with Glyphs

Any plot is usually made up of one or many geometrical shapes such as line, circle, rectangle, etc. These shapes have visual information about the corresponding set of data. In Bokeh terminology, these geometrical shapes are called gylphs. Bokeh plots constructed using bokeh.plotting interface use a default set of tools and styles. However, it is possible to customize the styles using available plotting tools.
Different types of plots created using glyphs are as given below −
This type of plot is useful for visualizing the movements of points along the x-and y-axes in the form of a line. It is used to perform time series analytics.
This is typically useful for indicating the count of each category of a particular column or field in your dataset.
This plot indicates a region of points in a particular shade of color. This type of plot is used to distinguish different groups within the same dataset.
This type of plot is used to visualize relationship between two variables and to indicate the strength of correlation between them.
Different glyph plots are formed by calling appropriate method of Figure class. The Figure object is obtained by following constructor −
from bokeh.plotting import figure
figure(**kwargs)

The Figure object can be customised by various keyword arguments.
The line() method of Figure object adds a line glyph to the Bokeh figure. It needs x and y parameters as data arrays for showing their linear relationship.
from bokeh.plotting import figure, show
fig = figure()
fig.line(x,y)
show(fig)

Following code renders a simple line plot between two sets of values in the form Python list objects −
from bokeh.plotting import figure, output_file, show
x = [1,2,3,4,5]
y = [2,4,6,8,10]
output_file('line.html')
fig = figure(title = 'Line Plot example', x_axis_label = 'x', y_axis_label = 'y')
fig.line(x,y)
show(fig)

The figure object has two different methods for constructing bar plot
The bars are shown horizontally across plot width. The hbar() method has the following parameters −
Following code is an example of horizontal bar using Bokeh.
from bokeh.plotting import figure, output_file, show
fig = figure(plot_width = 400, plot_height = 200)
fig.hbar(y = [2,4,6], height = 1, left = 0, right = [1,2,3], color = "Cyan")
output_file('bar.html')
show(fig)
The bars are shown vertically across plot height. The vbar() method has following parameters −
Following code displays vertical bar plot −
from bokeh.plotting import figure, output_file, show
fig = figure(plot_width = 200, plot_height = 400)
fig.vbar(x = [1,2,3], width = 0.5, bottom = 0, top = [2,4,6], color = "Cyan")
output_file('bar.html')
show(fig)

A plot which shades a region of space in a specific color to show a region or a group having similar properties is termed as a patch plot in Bokeh. Figure object has patch() and patches() methods for this purpose.
This method adds patch glyph to given figure. The method has the following arguments −
A simple patch plot is obtained by the following Python code −
from bokeh.plotting import figure, output_file, show
p = figure(plot_width = 300, plot_height = 300)
p.patch(x = [1, 3,2,4], y = [2,3,5,7], color = "green")
output_file('patch.html')
show(p)
This method is used to draw multiple polygonal patches. It needs following arguments −
As an example of patches() method, run the following code −
from bokeh.plotting import figure, output_file, show
xs = [[5,3,4], [2,4,3], [2,3,5,4]]
ys = [[6,4,2], [3,6,7], [2,4,7,8]]
fig = figure()
fig.patches(xs, ys, fill_color = ['red', 'blue', 'black'], line_color = 'white')
output_file('patch_plot.html')
show(fig)
Scatter plots are very commonly used to determine the bi-variate relationship between two variables. The enhanced interactivity is added to them using Bokeh. Scatter plot is obtained by calling scatter() method of Figure object. It uses the following parameters −
Following marker type constants are defined in Bokeh: −
Asterisk
Circle
CircleCross
CircleX
Cross
Dash
Diamond
DiamondCross
Hex
InvertedTriangle
Square
SquareCross
SquareX
Triangle
X
Following Python code generates scatter plot with circle marks.
from bokeh.plotting import figure, output_file, show
fig = figure()
fig.scatter([1, 4, 3, 2, 5], [6, 5, 2, 4, 7], marker = "circle", size = 20, fill_color = "grey")
output_file('scatter.html')
show(fig)
 Print
 Add Notes
 Bookmark this page