How to use bokeh in a Jupyter notebook without opening a separate window ?

Published: February 09, 2024

Tags: Python; Bokeh;

DMCA.com Protection Status

Introduction

Bokeh is a popular data visualization library that allows users to create interactive and visually appealing charts, plots, and graphs. One of the advantages of using Bokeh is its integration with Jupyter notebook, which allows for seamless creation of visualizations within your code.

However, by default, when creating Bokeh plots in a Jupyter notebook, it opens up a separate window to display the output. This can be inconvenient and disrupts the flow of your analysis. In this section, we will explore how to use Bokeh in a Jupyter notebook without opening a separate window.

Creating a bokeh plot in a Jupyter notebook

Let's take a look at the code snippet below:

from bokeh.plotting import figure, show

p = figure(width=600, height=600)

p.circle([1, 2, 3, 4, 5], 
         [1, 2, 3, 4, 5], 
         size=20, color="red", alpha=0.75)

show(p)

When executed in a Jupyter Notebook, it will generate a plot and open a new window (refer to the images below).

How to use bokeh in a Jupyter notebook without opening a separate window ?
How to use bokeh in a Jupyter notebook without opening a separate window ?

How to use bokeh in a Jupyter notebook without opening a separate window ?
How to use bokeh in a Jupyter notebook without opening a separate window ?

Using bokeh in a Jupyter notebook

One way to avoid opening a separate window when using Bokeh is by utilizing its output options. These options allow for different ways to display Bokeh plots, including inline within the Jupyter notebook.

To use this option, we need to import the output_notebook() function from bokeh.io. This will instruct Bokeh to display all its output within the notebook itself. Let's see an example of how this can be implemented:

from bokeh.plotting import figure, show
from bokeh.plotting import output_notebook

p = figure(width=600, height=600)

p.circle([1, 2, 3, 4, 5], 
         [1, 2, 3, 4, 5], 
         size=20, color="red", alpha=0.75)

output_notebook()

show(p)

If Jupyter Notebook continues to open a separate window even after adding output_notebook(), simply restart your Jupyter Notebook by clicking on the arrow as shown in the image below.

How to use bokeh in a Jupyter notebook without opening a separate window ?
How to use bokeh in a Jupyter notebook without opening a separate window ?

References

Links Site
output_notebook() docs.bokeh.org
Image

of