How can Bokeh library be used to generate line graphs in Python?

Bokeh is a Python package that helps in data visualization. It is an open source project. Bokeh renders its plots using HTML and JavaScript. This indicates that it is useful while working with web−based dashboards.

Visualizing data is an important step since it helps understand what is going on in the data without actually looking at the numbers and performing complicated computations.

Bokeh can be easily used in conjunction with NumPy, Pandas, and other Python packages. It can be used to produce interactive plots, dashboards, and so on. It helps in communicating the quantitative insights to the audience effectively.

How Bokeh Works

Bokeh converts the data source into a JSON file. This file is used as an input to BokehJS, which is a JavaScript library. This BokehJS is written in TypeScript that helps render visualization on modern browsers.

Matplotlib and Seaborn produce static plots, whereas Bokeh produces interactive plots. This means when the user interacts with these plots, they change accordingly.

Plots can be embedded as output of Flask or Django enabled web applications. Jupyter notebook can also be used to render these plots.

Installation

Installation of Bokeh on Windows command prompt ?

pip3 install bokeh

Installation of Bokeh on Anaconda prompt ?

conda install bokeh

Creating a Simple Line Plot

The following example demonstrates how to create a basic line plot using Bokeh ?

from bokeh.plotting import figure, output_file, show

# Sample data points
x = [1, 2, 3, 4, 5, 6]
y = [2, 5, 8, 1, 2, 4]

# Specify output file
output_file('line_plot.html')

# Create figure with title and axis labels
fig = figure(title='Line Plot Example', x_axis_label='X Values', y_axis_label='Y Values')

# Add line renderer
fig.line(x, y, line_width=2, color='blue')

# Show the plot
show(fig)

Advanced Line Plot Features

Bokeh allows customization of line plots with various styling options ?

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

# Generate sample data
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)

# Specify output file
output_file('advanced_line_plot.html')

# Create figure with custom dimensions
fig = figure(title='Sine Wave', x_axis_label='X', y_axis_label='Sin(X)', 
             width=600, height=400)

# Add line with custom styling
fig.line(x, y, line_width=3, color='red', alpha=0.8, legend_label='sin(x)')

# Add circles at data points
fig.circle(x[::10], y[::10], size=8, color='blue', alpha=0.5)

# Show the plot
show(fig)

Multiple Lines on Same Plot

You can add multiple lines to the same plot for comparison ?

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

# Generate data
x = np.linspace(0, 4*np.pi, 50)
y1 = np.sin(x)
y2 = np.cos(x)

# Create output file
output_file('multiple_lines.html')

# Create figure
fig = figure(title='Sine and Cosine Waves', x_axis_label='X', y_axis_label='Y')

# Add multiple lines
fig.line(x, y1, line_width=2, color='blue', legend_label='sin(x)')
fig.line(x, y2, line_width=2, color='red', legend_label='cos(x)')

# Configure legend
fig.legend.location = "top_right"

# Show plot
show(fig)

Conclusion

Bokeh is a powerful library for creating interactive line plots in Python. It offers extensive customization options and generates web−ready HTML output, making it ideal for dashboards and web applications.

Updated on: 2026-03-25T15:04:06+05:30

239 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements