Plotting with Bokeh#

Arviz has the option to use Bokeh as backend which offers interactive plots. Although most of the functions in the plot module work seamlessly with any backend, some advanced plots may require the use of backend specific features. In this guide, advanced plotting with Bokeh will be covered.

This page can be downloaded as a Jupyter notebook.

import arviz as az
import matplotlib.pyplot as plt
import numpy as np

az.style.use("arviz-doc")

# Confgure Bokeh as backend
az.rcParams["plot.backend"] = "bokeh"
az.output_notebook()
Loading BokehJS ...

Note

To display the plots as output in a Jupyter notebook, it’s required to call the function arviz.output_notebook() beforehand.

Customizing plots#

Using backend_kwargs#

The backend_kwargs argument can be very useful for some specific configuration. That is parameters available in bokeh:bokeh.plotting.figure.Figure. As the options available depends on the backend, this parameter is not as flexible as creating custom axes.

As an example, the following code changes the color background and the plot width.

# load data
data = az.load_arviz_data('radon')
az.plot_posterior(
    data,
    var_names=["g"],
    backend_kwargs={"width": 350,
                    "background_fill_color": "#d3d0e3"});

The parameter show#

The parameter show is used to control whether the plot is displayed or not. This behavior can be useful when a plot will be displayed in a grid as the example in the following section.

Defining custom axes#

The ax argument of any plot function allows to use created axes manually. In the example, this parameter allows to arrange 2 different plots in a grid, set limit to the x axis and share the axes’ ranges between the two plots.

from bokeh.io import show
from bokeh.layouts import row
from bokeh.plotting import figure

# load data
observed_data = data.observed_data.y.to_numpy()
# create axes
f1 = figure(x_range=(observed_data.min() - 1, observed_data.max() + 1))
f2 = figure(x_range=f1.x_range, y_range=f1.y_range)
# plot
az.plot_ppc(data, group="prior", num_pp_samples=100, show=False, ax=f1)
az.plot_ppc(data, group="posterior", num_pp_samples=100, show=False, ax=f2)

az.show_layout([[f1], [f2]])