Last modified: Jan 01, 2025 By Alexander Williams

Python Bokeh Figure: Creating Interactive Plots

Before diving into creating plots with Bokeh's figure() function, make sure you have Bokeh installed. If you haven't installed it yet, check out our guide on how to install and get started with Python Bokeh.

Understanding Bokeh Figure Basics

The figure() function is the cornerstone of creating visualizations in Bokeh. It creates a new figure object that serves as a container for all your plotting elements.

Basic Figure Creation


from bokeh.plotting import figure, show

# Create a basic figure
p = figure(width=600, height=400, title="My First Bokeh Plot")

# Add a line plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
p.line(x, y)

# Display the plot
show(p)

Essential Figure Parameters

When creating a figure, you can customize various aspects using these important parameters:

  • width: Plot width in pixels
  • height: Plot height in pixels
  • title: Plot title
  • x_axis_label: Label for x-axis
  • y_axis_label: Label for y-axis

Customized Figure Example


# Create a more customized figure
p = figure(
    width=800,
    height=400,
    title="Sales Data Visualization",
    x_axis_label='Month',
    y_axis_label='Sales ($)',
    background_fill_color='#f5f5f5',
    tools='pan,box_zoom,reset,save'
)

Adding Interactive Tools

Bokeh's interactive features make it stand out from other plotting libraries. You can add various tools to enhance user interaction:


# Create figure with specific tools
p = figure(
    tools='pan,wheel_zoom,box_select,reset',
    active_drag="box_select"
)

# Add more tools after creation
from bokeh.models import HoverTool
hover = HoverTool(tooltips=[
    ('x', '@x'),
    ('y', '@y')
])
p.add_tools(hover)

Multiple Plots in One Figure

You can add multiple plots to the same figure to create complex visualizations. Here's an example combining different plot types:


import numpy as np

# Create data
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
y2 = np.cos(x)

# Create figure with multiple plots
p = figure(width=600, height=400, title="Trigonometric Functions")

# Add multiple plots
p.line(x, y1, legend_label="sin(x)", line_color="blue")
p.line(x, y2, legend_label="cos(x)", line_color="red")
p.circle(x, y1, size=8, legend_label="sin(x) points", fill_color="blue")

# Configure legend
p.legend.click_policy="hide"
show(p)

Customizing Plot Appearance

Bokeh offers extensive customization options for your figures. Here's how to style various elements:


# Create a styled figure
p = figure(
    width=600, 
    height=400,
    title="Styled Plot",
    background_fill_color='#fafafa',
    border_fill_color='whitesmoke',
    title_location='above',
    title_text_font_size='20px',
    title_text_font_style='bold'
)

# Customize axes
p.xaxis.axis_label_text_color = 'navy'
p.yaxis.axis_label_text_color = 'navy'
p.xgrid.grid_line_color = 'gray'
p.ygrid.grid_line_color = 'gray'

Handling Common Issues

If you encounter any issues with importing Bokeh or creating figures, refer to our guide on fixing the No Module Named Bokeh Error.

Best Practices

When working with Bokeh figures, keep these best practices in mind:

  • Always specify figure dimensions for consistent display
  • Use meaningful titles and axis labels
  • Include appropriate interactive tools for your use case
  • Consider color-blind friendly color schemes
  • Add legends when plotting multiple data series

Conclusion

Bokeh's figure() function provides a powerful foundation for creating interactive visualizations. Understanding its parameters and capabilities is essential for effective data visualization in Python.

With proper customization and tool selection, you can create professional-looking, interactive plots that effectively communicate your data insights.