Bokeh – Layouts

Bokeh - Layouts

In this chapter, we will discuss Bokeh – Layouts. These layouts as well as sizing modes result in plots and widgets resizing automatically as per the size of the browser window. For consistent appearance, all items in a layout must have the same sizing mode.

The first type of layout is Column layout which displays plot figures vertically. layouts module and takes the following signature −

from bokeh.layouts import column
col = column(children, sizing_mode)

children − List of plots and/or widgets.

sizing_mode − determines how items in the layout resize.

The following code produces two Bokeh figures and places them in a column layout so that they are displayed vertically. Line glyphs representing the sine.

from bokeh.plotting import figure, output_file, show
from bokeh.layouts import column
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
y1 = np.sin(x)
y2 = np.cos(x)
fig1 = figure(plot_width = 200, plot_height = 200)
fig1.line(x, y1,line_width = 2, line_color = 'blue')
fig2 = figure(plot_width = 200, plot_height = 200)
fig2.line(x, y2,line_width = 2, line_color = 'red')
c = column(children = [fig1, fig2], sizing_mode = 'stretch_both')
show(c)

Output

Bokeh - Layouts

Similarly, the Row layout arranges plots horizontally, for which row() functions as defined in bokeh. layouts module is used. As you would think, it also takes two arguments (similar to column() function) – children and sizing_mode.

The sine and cos curves as shown vertically in the above diagram are now displayed horizontally in row layout with the following code

from bokeh.plotting import figure, output_file, show
from bokeh.layouts import row
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
y1 = np.sin(x)
y2 = np.cos(x)
fig1 = figure(plot_width = 200, plot_height = 200)
fig1.line(x, y1,line_width = 2, line_color = 'blue')
fig2 = figure(plot_width = 200, plot_height = 200)
fig2.line(x, y2,line_width = 2, line_color = 'red')
r = row(children = [fig1, fig2], sizing_mode = 'stretch_both')
show(r)

Output

Bokeh - Layouts

The Bokeh package also has a grid layout. It holds multiple plot figures (as well as widgets) in a two-dimensional grid of rows and columns. The grid plot() function in bokeh. layouts module returns a grid and a single unified toolbar which may be positioned with the help of toolbar_location property.

This is unlike row or column layout where each plot shows its own toolbar. The grid() function uses children and sizing_mode parameters where the child is a list of lists. Ensure that each sublist is of the same dimensions.

In the following code, four different relationships between x and y data series are plotted in a grid of two rows and two columns.

from bokeh.plotting import figure, output_file, show
from bokeh.layouts import gridplot
import math
x = list(range(1,11))

y1 = x
y2 =[11-i for i in x]
y3 = [i*i for i in x]
y4 = [math.log10(i) for i in x]

fig1 = figure(plot_width = 200, plot_height = 200)
fig1.line(x, y1,line_width = 2, line_color = 'blue')
fig2 = figure(plot_width = 200, plot_height = 200)
fig2.circle(x, y2,size = 10, color = 'green')
fig3 = figure(plot_width = 200, plot_height = 200)
fig3.circle(x,y3, size = 10, color = 'grey')
fig4 = figure(plot_width = 200, plot_height = 200, y_axis_type = 'log')
fig4.line(x,y4, line_width = 2, line_color = 'red')
grid = gridplot(children = [[fig1, fig2], [fig3,fig4]], sizing_mode = 'stretch_both')
show(grid)

Output

Bokeh - Layouts

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply