Last modified: Jan 01, 2025 By Alexander Williams

Python Bokeh Line Plot Tutorial with Examples

In this comprehensive guide, we'll explore how to create interactive line plots using the Bokeh library in Python. Before diving in, make sure you have Bokeh properly installed in your environment.

Basic Line Plot Creation

The line() method in Bokeh allows you to create line plots by specifying x and y coordinates. Let's start with a simple example:


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

# Create sample data
x = np.linspace(0, 10, 50)
y = np.sin(x)

# Create a new figure
p = figure(title='Simple Line Plot', x_axis_label='x', y_axis_label='y')

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

# Show the plot
show(p)

Customizing Line Properties

You can enhance your line plots with various customization options. Let's explore multiple lines with different styles:


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

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

# Create figure
p = figure(title='Multiple Lines Plot')

# Add different styled lines
p.line(x, y1, line_color='blue', line_width=2, legend_label='sin(x)')
p.line(x, y2, line_color='red', line_dash='dashed', line_width=2, legend_label='cos(x)')

# Configure legend
p.legend.location = 'top_right'
p.legend.click_policy = 'hide'

show(p)

Interactive Features

One of Bokeh's strengths is its interactive features. You can create interactive plots with hover tools:


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

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

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

# Add line with hover functionality
p.line(x, y, line_width=3)

show(p)

Multiple Data Series

You can plot multiple data series to compare different datasets. Here's an example showing temperature trends:


from bokeh.plotting import figure, show
from bokeh.palettes import Spectral3

# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
city1_temps = [20, 24, 27, 32, 35]
city2_temps = [15, 18, 22, 25, 28]

# Create figure
p = figure(title='Temperature Comparison', x_range=months)

# Plot multiple lines
p.line(months, city1_temps, line_color=Spectral3[0], legend_label='City 1')
p.line(months, city2_temps, line_color=Spectral3[1], legend_label='City 2')

# Customize plot
p.xgrid.grid_line_color = None
p.legend.location = "top_left"

show(p)

Saving and Sharing Plots

After creating your line plots, you can save them as HTML files for sharing or embedding in web pages:


from bokeh.plotting import figure, save
from bokeh.io import output_file

# Create basic plot
p = figure(title='Saved Line Plot')
p.line([1, 2, 3], [4, 5, 6])

# Save the plot
output_file("line_plot.html")
save(p)

Best Practices and Tips

When creating line plots with Bokeh, keep these important considerations in mind:

- Always provide clear axis labels and titles for better readability

- Use appropriate line colors and styles to distinguish multiple series

- Include interactive features when they add value to data interpretation

- Optimize plot size and layout for your target display medium

Conclusion

Bokeh's line() method provides a powerful way to create interactive and visually appealing line plots. With its extensive customization options and interactive features, you can create professional-looking visualizations for your data analysis needs.

Remember to experiment with different properties and features to create the most effective visualizations for your specific use case. The interactive nature of Bokeh plots makes them particularly valuable for web-based data presentation.