# 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

```python
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show, ColumnDataSource
from bokeh.io import output_notebook
output_notebook()
```

&#x20;Loading BokehJS ...

```python
#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)
```

![Plot 1](https://3769175098-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJvb9AjGRgb32WfBeFy%2F-MLrAiU9xGNMOeYISZ1w%2F-MLrqqQti2N75O0WgDFE%2FScreenshot%202020-11-11%20at%2016.08.10.png?alt=media\&token=0ee0ae8a-ba6f-4e6a-9800-0de22f1c1fa2)

````python
#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) ```
````

![Plot 2](https://3769175098-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJvb9AjGRgb32WfBeFy%2F-MLrAiU9xGNMOeYISZ1w%2F-MLrpf-02Vo1qwjNbMFa%2Fplot2.png?alt=media\&token=cdaaf7a6-c36f-4bee-a722-8e82133c6924)

```python
#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)
```

![plot 3](https://3769175098-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJvb9AjGRgb32WfBeFy%2F-MLrAiU9xGNMOeYISZ1w%2F-MLrpWveNCloHL6kPzOx%2Fplot3.png?alt=media\&token=b217d2e3-622b-4d18-872c-f627fa09ab02)

## Plots in a row layout&#x20;

In order to plot three plots in a horizontal row we use code as shown below

````python
from bokeh.layouts import row 
#group 3 plots in a 'row' layout 
row_layout = row(plot1, plot2, plot3) 
# output the plot 
show(row_layout) ```
````

![](https://3769175098-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJvb9AjGRgb32WfBeFy%2F-MLrAiU9xGNMOeYISZ1w%2F-MLrrpLPWcaNJ0k9-uXQ%2FScreenshot%202020-11-11%20at%2016.08.19.png?alt=media\&token=7c4a1218-0d68-4a2b-8883-9922233b6884)

## 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`

```python
from bokeh.layouts import column

#group 3 plots in a 'row' layout

column_layout = column(plot1, plot2, plot3)

# output the plot
show(column_layout)
```

## &#x20;Plot in a nested layout&#x20;

![Plot in a grid, similar to nested layout, but only one](https://3769175098-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJvb9AjGRgb32WfBeFy%2F-MLruUzSYwm4CRo5URyy%2F-MLrvrBSid0hTr5_aHdb%2FScreenshot%202020-11-11%20at%2016.09.25.png?alt=media\&token=6f17e93c-fc38-41b8-8e76-97e268320349)

Combining the two can create a nested layout

```python
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

```python
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)
```

![plot in a tabbed layout](https://3769175098-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJvb9AjGRgb32WfBeFy%2F-MLruUzSYwm4CRo5URyy%2F-MLrvUwRJ9PT6vG1xMUr%2FScreenshot%202020-11-11%20at%2016.29.58.png?alt=media\&token=0b2c402b-8d04-4593-8db5-d0e5cc69b0be)

In the example below we show one child on one tab, but two childs by the use of `column` on the other tab

```python
 #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

```python
from bokeh.layouts import gridplot
show(gridplot([plot1, plot2, plot3], ncols=2, plot_width=450, plot_height=300))
```

## &#x20;Linking plots together&#x20;

Sometimes you want to compare plots with each other and therefore,  you need the same y-range. The following code illustrates this

```python
plot1.y_range = plot2.y_range
row_layout = row(plot1, plot2)
show(row_layout) 
```

![plots with same y-axis](https://3769175098-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJvb9AjGRgb32WfBeFy%2F-MLruUzSYwm4CRo5URyy%2F-MLrugH1MCr9BhKYWfxi%2FScreenshot%202020-11-11%20at%2016.09.39.png?alt=media\&token=6e4ac080-7978-428b-b9c7-3b62bde2a431)
