Last modified: Jan 01, 2025 By Alexander Williams

Python Bokeh show(): Visualize Plots in Browser

The Bokeh show() function is a crucial component for visualizing plots in Python. Before diving deep into this topic, make sure you have Bokeh properly installed on your system.

Understanding Bokeh show() Function

The show() function in Bokeh is responsible for rendering and displaying your plots either in a web browser or Jupyter notebook. It's the final step in the visualization process.

Basic Usage of show()

Here's a simple example of how to create and display a basic plot:


from bokeh.plotting import figure, show

# Create a new plot
p = figure(width=400, height=300, title="Basic Example")

# Add a line to the plot
p.line([1, 2, 3, 4, 5], [2, 5, 8, 2, 7])

# Display the plot
show(p)

Display Options and Configurations

When using show(), you can specify various parameters to control how your plot is displayed. Here's a more detailed example:


from bokeh.plotting import figure, show
from bokeh.layouts import column

# Create multiple plots
p1 = figure(width=300, height=200, title="Plot 1")
p1.circle([1, 2, 3], [4, 5, 6])

p2 = figure(width=300, height=200, title="Plot 2")
p2.line([1, 2, 3], [7, 8, 9])

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

Browser vs. Notebook Display

The show() function automatically detects your environment. When using Jupyter notebooks, you'll need to load the Bokeh extension first:


from bokeh.io import output_notebook
output_notebook()  # Enable notebook display

# Your plotting code here
p = figure(title="Notebook Example")
p.circle([1, 2, 3], [4, 5, 6])
show(p)

Advanced Features and Best Practices

When working with complex visualizations, you might want to create interactive plots with multiple elements. Here's an example:


from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, HoverTool

# Create data source
source = ColumnDataSource(data={
    'x': [1, 2, 3, 4, 5],
    'y': [2, 5, 8, 2, 7],
    'desc': ['A', 'B', 'C', 'D', 'E']
})

# Create plot with hover tool
p = figure(title="Interactive Plot")
p.add_tools(HoverTool(tooltips=[
    ('x', '@x'),
    ('y', '@y'),
    ('Description', '@desc')
]))

p.circle('x', 'y', size=10, source=source)
show(p)

Troubleshooting Common Issues

If you encounter issues with show(), ensure you have all dependencies installed correctly. If you see import errors, refer to our guide on fixing Bokeh import errors.

Common issues include:

  • Plot not displaying: Check your browser's pop-up settings
  • Blank output: Verify JavaScript is enabled in your browser
  • Layout issues: Ensure proper sizing parameters are set

Performance Optimization

When working with large datasets, consider these optimization techniques:


from bokeh.plotting import figure, show
from bokeh.layouts import layout
import numpy as np

# Create efficient data structure
x = np.linspace(0, 10, 1000)
y = np.sin(x)

# Create plot with optimized parameters
p = figure(title="Optimized Plot", sizing_mode="stretch_width")
p.line(x, y, line_color="navy", line_alpha=0.6)

show(p)

Conclusion

The Bokeh show() function is essential for visualizing your plots. Understanding its proper usage and options will help you create effective and interactive data visualizations.

Remember to always consider your target environment (browser or notebook) and implement appropriate error handling for robust applications.