How can multiple lines be visualized using Bokeh Python?

Bokeh is a Python package that helps in data visualization. It is an open source project that renders plots using HTML and JavaScript, making it useful for web-based dashboards and interactive visualizations.

Unlike Matplotlib and Seaborn which produce static plots, Bokeh creates interactive plots that respond to user interactions. The multi_line() method allows you to display multiple lines with different properties on a single plot.

Installation

Install Bokeh using pip or conda ?

pip install bokeh

Or using Anaconda ?

conda install bokeh

Basic Multi−Line Plot

The multi_line() method takes lists of x and y coordinates for each line ?

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

# Create figure
p = figure(width=500, height=300, title="Multiple Lines Example")

# Define data for multiple lines
x_coords = [[1, 2, 3, 4], [2, 3, 4, 5]]
y_coords = [[2, 5, 3, 8], [1, 4, 6, 2]]

# Add multiple lines
p.multi_line(x_coords, y_coords,
             color=["red", "blue"], 
             alpha=[0.8, 0.6], 
             line_width=3)

show(p)

Advanced Multi−Line with Different Styles

You can customize each line with different colors, widths, and transparency ?

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

# Create sample data
x1 = np.linspace(0, 4*np.pi, 100)
x2 = np.linspace(0, 4*np.pi, 100)
y1 = np.sin(x1)
y2 = np.cos(x2)
y3 = np.sin(x1) * np.cos(x1)

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

# Plot multiple lines
p.multi_line([x1, x2, x1], [y1, y2, y3],
             color=["red", "green", "blue"],
             alpha=[0.8, 0.8, 0.8],
             line_width=[2, 3, 2],
             legend_label="Trig Functions")

p.legend.location = "top_right"
show(p)

Multi−Line Parameters

Parameter Description Example
xs, ys Lists of x, y coordinates [[1,2,3], [4,5,6]]
color Line colors ["red", "blue"]
alpha Transparency (0-1) [0.8, 0.6]
line_width Line thickness [2, 4]
X-axis Y-axis Multiple Lines Visualization Line 1 Line 2 Line 3

Key Features

Interactive Plots: Bokeh plots are interactive by default, supporting zoom, pan, and hover tools.

Web Integration: Plots can be embedded in Flask or Django web applications and rendered in Jupyter notebooks.

Customization: Each line can have different styling properties like color, width, and transparency.

Conclusion

Bokeh's multi_line() method provides an efficient way to visualize multiple datasets on a single interactive plot. Use different colors and styles to distinguish between lines, and leverage Bokeh's interactivity for better data exploration.

Updated on: 2026-03-25T15:29:08+05:30

600 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements