Last modified: 12 December 2024

Starting the bokeh backend


New in CIAO 4.16 is support for plotting with the Bokeh backend rather than Matplotlib. This is useful for people who use Jupyter notebooks for their analysis, as bokeh requires a webserver to create plots for interactive analysis.

After installing bokeh with

% pip install bokeh

and, for Jupyter lab users,

% pip install jupyter_bokeh

then the bokeh backend can be selected using the set_plot_backend call. Note that Bokeh works differently to Matlpotlib which means that you will need to explicitly display the plot at the end of each Jupyter cell.

So, with the commands

from sherpa.astro import ui
from bokeh.plotting import show
import sherpa.plot

then the Bokeh backend can be selected with

ui.set_plot_backend("BokehBackend")

The notebook support must be started before a plot is created with:

from bokeh.io import output_notebook
output_notebook()

After this, plots can be created with the normal Sherpa plot commands like plot_data, but the show command shown below is needed to display the visualization:

ui.load_arrays(1, [1, 2, 3], [10, 2, 20])
ui.plot_data()
show(sherpa.plot.backend.current_fig)

It is suggested that you use a simple helper function like the display function below:

def display():
    show(sherpa.plot.backend.current_fig)