As a part of this tutorial, we'll be covering various ways to layout bokeh charts to create figure combining analysis on a particular topic.
bokeh.layouts provides various ways to layout charts in order to create images. Below we have listed down layouts that we'll be explaining as a part of this tutorial:
We'll start by importing necessary libraries.
from bokeh.io import output_notebook, show from bokeh.plotting import figure from bokeh.resources import INLINE
We'll be generating various charts using the
autompg dataset that is readily available with bokeh.
Bokeh provides this dataset as a part of the
sampledata module as a pandas dataframe.
from bokeh.sampledata.autompg import autompg from bokeh.sampledata.iris import flowers
|0||18.0||8||307.0||130||3504||12.0||70||1||chevrolet chevelle malibu|
|1||15.0||8||350.0||165||3693||11.5||70||1||buick skylark 320|
|3||16.0||8||304.0||150||3433||12.0||70||1||amc rebel sst|
We'll now go through each layout by creating various charts using the above datasets and lining them using the layout.
The Row layout as its name suggests let us organize all charts in a row. The
bokeh.layouts module provides a method named
row() which accepts a list of charts as input and plots all of them in a row. We have explained below its usage with a simple example.
from bokeh.layouts import row ## Chart 1 - Scatter Plot chart1 = figure(width=300, height=300) chart1.circle( x=autompg["mpg"], y=autompg["hp"], size=8, alpha=0.9, fill_color="lawngreen", line_color="tomato" ) chart1.xaxis.axis_label="MPG" chart1.yaxis.axis_label="Horsepower" ## Chart 2 - Bar Chart autompg_avg_by_origin = autompg.groupby(by="origin").mean() chart2 = figure(width=300, height=300) chart2.vbar( x = [1,2,3], width=0.8, top=autompg_avg_by_origin.mpg, fill_color="dodgerblue", line_color="dodgerblue", alpha=0.9 ) chart2.xaxis.axis_label="Region" chart2.yaxis.axis_label="MPG" chart2.xaxis.ticker = [1, 2, 3] ## Chart 3 - Horizontal Bar Chart chart3 = figure(width=300, height=300) chart3.hbar( y = [1,2,3], height=0.8, right=autompg_avg_by_origin.mpg, fill_color="tomato", line_color="red") chart3.xaxis.axis_label="MPG" chart3.yaxis.axis_label="Region" chart3.yaxis.ticker = [1, 2, 3] show(row(chart1, chart2, chart3))
The column layout lets us organize charts in a column-wise fashion. The
bokeh.layouts module provides a method named
column() which accepts a list of charts as input and plots all of them in a single column. We have used the same 3 charts from the previous example and have organized them as a column.
from bokeh.layouts import column show(column(chart1, chart2, chart3))
We can even combine row and column layouts by giving each of them as an argument to another to create a complicated layout according to our choice. Below we are creating row layout of
chart3 and then passing it as 2nd chart to column layout method.
show(column(chart1 , row(chart2, chart3)))
The grid layout lets us organize things in the
NxN grid. The
bokeh.layouts module provides 2 method named
grid() for creating grid layout in bokeh. It accepts lists of a list containing charts as input. We can omit cells of the
NxN grid where we don't want to include a chart. We can even stretch charts to more than one cell of the grid.
Below we have created a
2x2 grid of charts where we are passing None for the first chart of cell and we can see a gap in the result.
from bokeh.layouts import gridplot grid_layout = gridplot([[None, chart1], [chart2, chart3]], plot_width=300, plot_height=300) show(grid_layout)
We can also pass all charts as a single list and then enforce a number of columns using the
ncols attribute of the
grid_layout = gridplot([chart1,chart2, chart3], ncols=2) show(grid_layout)
We have below added one more chart to layout and filled all cells of the
chart4 = figure(width=300, height=300) chart4.square( x=autompg["accel"], y=autompg["displ"], size=8, alpha=0.9, fill_color="orange", line_color="black" ) chart4.xaxis.axis_label="Acceleration" chart4.yaxis.axis_label="Disposition" grid_layout = gridplot([[chart1, chart4], [chart2, chart3]], plot_width=300, plot_height=300) show(grid_layout)
grid() method works exactly like
gridplot(). It stretches all charts if we do not provide a chart for any cell of the grid. It also stretches charts to the full length of the screen.
sizing_mode attribute to handle charts with different sizes by scaling or stretching them to make layout look good. It accepts one of the below values:
from bokeh.layouts import grid grid_layout = grid([[chart1],[chart2, chart3]]) show(grid_layout)
grid() method provides
ncols attributes which can let us force the
MxN grid layout on charts passed as list.
grid_layout = grid([chart1,chart2, chart3], nrows=2) show(grid_layout)
The last layout available through the bokeh is a custom layout. It accepts a list of list of charts as input and lays out each list of charts in a single row. The
bokeh.layouts module provides
layout() method for this. It accepts a list of list of charts as the first input. This lets us organize things according to our needs.
We can use it's
sizing_mode attributes to handle charts with different sizes by scaling or stretching them to make the layout look good.
from bokeh.layouts import layout figure_layout = layout([chart1, [chart2, chart3]], sizing_mode="scale_height") show(figure_layout)