Bokeh layouts
In order to make good graphical analysis and make plots visually attractive you can make use of layouts. The use of layouts
, tabs
, and grids
enhances your plots and create possibilities like using the same axes.
For demonstration purpose, we first create 3 individual plots of different shapes and heights
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show, ColumnDataSource
from bokeh.io import output_notebook
output_notebook()
Loading BokehJS ...
#Create plot 1
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
plot1 = figure(x_range=fruits, plot_height=250, plot_width=400,
title="Fruit Counts",
toolbar_location=None, tools="")
plot1.vbar(x=fruits, top=counts, width=0.9, color = "#c9d9d3")
show(plot1)

#Create plot 2
df = pd.read_csv("data/heart_failure_clinical_records_dataset.csv")
data = ColumnDataSource(data = {'x': df['ejection_fraction'], 'y': df['serum_sodium']})
plot2 = figure(y_axis_label = 'sodium serum (mEq/L)', x_axis_label='% ejection fraction', plot_height=250, plot_width=500) plot2.cross(x = 'x', y = 'y', source = data, color = 'green', size = 10, alpha = 0.5)
show(plot2) ```

#Create plot 3
N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
# Set up plot
plot3 = figure(plot_height=300, plot_width=400, title="my sine wave",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])
plot3.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
show(plot3)

Plots in a row layout
In order to plot three plots in a horizontal row we use code as shown below
from bokeh.layouts import row
#group 3 plots in a 'row' layout
row_layout = row(plot1, plot2, plot3)
# output the plot
show(row_layout) ```

Plots in a column layout
We can see that the three plots are simply stacked together side by side. If we would like the plots stacked vertically we should use column
from bokeh.layouts import column
#group 3 plots in a 'row' layout
column_layout = column(plot1, plot2, plot3)
# output the plot
show(column_layout)
Plot in a nested layout

Combining the two can create a nested layout
nested_layout = column(row(plot1, plot2), plot3)
show(nested_layout)
Tabbed layout
It might be more efficient to view a single plot a time but have multiple plots on a panel. Bokeh offers the tab layout, where each plot is stored in a single tab and it can be assessed by clicking on the tab
from bokeh.models import Tabs, Panel
#create the panels
tab1 = Panel(child = plot1, title = 'Tab one')
tab2 = Panel(child = plot2, title = 'Tab two')
tab3 = Panel(child = plot3, title = 'Tab three')
#feed the tabs into the Tabs objec
tabs_object = Tabs(tabs = [tab1, tab2, tab3])
show(tabs_object)

In the example below we show one child on one tab, but two childs by the use of column
on the other tab
#create the panels
tab1 = Panel(child = plot1, title = 'Tab one')
tab2 = Panel(child = column(plot2, plot3), title = 'Tab two')
#feed the tabs into the Tabs objec
tabs_object = Tabs(tabs = [tab1, tab2])
show(tabs_object)
Plot in a grid layout
A gird layout combines rows and columns and nested layouts and allows you to create plots in several directions. The grid layout is more versatile than the nested layout and you can create attractive layouts
from bokeh.layouts import gridplot
show(gridplot([plot1, plot2, plot3], ncols=2, plot_width=450, plot_height=300))
Linking plots together
Sometimes you want to compare plots with each other and therefore, you need the same y-range. The following code illustrates this
plot1.y_range = plot2.y_range
row_layout = row(plot1, plot2)
show(row_layout)

Last updated