Last modified: Jan 01, 2025 By Alexander Williams

Display Bokeh Plots in Jupyter with output_notebook()

The output_notebook() function in Bokeh enables seamless integration of interactive plots directly within Jupyter notebooks, enhancing your data visualization workflow.

Understanding output_notebook() Function

Before creating visualizations in Jupyter notebooks with Bokeh, you'll need to properly set up your environment. If you haven't installed Bokeh yet, check out how to install and get started with Python Bokeh.

Basic Setup for Jupyter Notebooks


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

# Enable notebook output
output_notebook()

Creating Your First Notebook Plot

Let's create a simple interactive plot to demonstrate how output_notebook() works in practice. We'll create a basic line plot with some sample data.


# Create sample data
x = [1, 2, 3, 4, 5]
y = [2, 5, 8, 2, 7]

# Create a new figure
p = figure(title="Simple Line Plot", x_axis_label='X Axis', y_axis_label='Y Axis')

# Add a line to the figure
p.line(x, y, line_width=2)

# Display the plot in the notebook
show(p)

Advanced Features and Customization

You can combine output_notebook() with Bokeh's interactive features to create more dynamic visualizations.


from bokeh.models import ColumnDataSource
from bokeh.layouts import column

# Create interactive data source
source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7]
))

# Create multiple plots
p1 = figure(title="Scatter Plot")
p1.circle('x', 'y', source=source, size=10)

p2 = figure(title="Line Plot")
p2.line('x', 'y', source=source)

# Show both plots in a column layout
show(column(p1, p2))

Common Issues and Solutions

Sometimes you might encounter issues when displaying plots in Jupyter notebooks. Here are some common problems and their solutions:

1. Plot Not Displaying

If your plot isn't showing up, ensure you've called output_notebook() before creating any plots. You may also need to restart your kernel if you've recently installed Bokeh.

2. Multiple Output Backends

When switching between notebook and HTML file output, you'll need to be explicit about your choice. You can use output_file() for HTML files.


# Reset output settings
from bokeh.io import reset_output

# Clear previous settings
reset_output()

# Set new output mode
output_notebook()

Best Practices

Here are some recommended practices when using output_notebook():

  • Always call output_notebook() at the beginning of your notebook
  • Use reset_output() when switching between different output backends
  • Consider using separate cells for plot creation and display

Performance Optimization

For better performance when working with large datasets or multiple plots, consider these tips:


# Use webdriver for better performance with large datasets
output_notebook(webdriver="firefox")  # Requires selenium and geckodriver

# Optimize data loading
from bokeh.models import DataRange1d
p = figure(x_range=DataRange1d(start=0, end=100))

Conclusion

The output_notebook() function is a powerful tool for creating interactive visualizations directly in Jupyter notebooks. It provides a seamless way to integrate Bokeh's capabilities with your data analysis workflow.

Whether you're creating simple plots or complex interactive dashboards, understanding how to properly use output_notebook() is essential for effective data visualization in Jupyter environments.