How can Bokeh be used to generate sinusoidal waves in 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.

Visualizing data is an important step since it helps understand patterns in the data without performing complicated computations. Bokeh produces interactive plots that change when users interact with them, unlike static plots from Matplotlib and Seaborn.

Installation

Install Bokeh using pip or conda ?

pip3 install bokeh

Or using Anaconda ?

conda install bokeh

Generating Sinusoidal Waves

Let's create a sine wave visualization using Bokeh's plotting interface ?

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

# Generate x values from 0 to 4?
x = np.arange(0, math.pi*4, 0.1)
y = np.sin(x)

# Specify output file
output_file("sine_wave.html")

# Create figure with labels
p = figure(title="A Simple Sine Wave", 
           x_axis_label='x', 
           y_axis_label='y',
           width=600, 
           height=400)

# Add line renderer
p.line(x, y, legend_label="sine", line_width=2, line_color="blue")

# Display the plot
show(p)

Creating Multiple Waves

You can plot multiple sinusoidal waves with different frequencies and amplitudes ?

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

x = np.arange(0, math.pi*4, 0.1)

# Different sinusoidal waves
y1 = np.sin(x)
y2 = np.sin(2*x)
y3 = 0.5 * np.sin(3*x)

output_file("multiple_waves.html")

p = figure(title="Multiple Sinusoidal Waves", 
           x_axis_label='x', 
           y_axis_label='y',
           width=700, 
           height=400)

# Plot multiple lines
p.line(x, y1, legend_label="sin(x)", line_width=2, line_color="blue")
p.line(x, y2, legend_label="sin(2x)", line_width=2, line_color="red")
p.line(x, y3, legend_label="0.5*sin(3x)", line_width=2, line_color="green")

# Position legend
p.legend.location = "top_right"

show(p)

How It Works

  • Import required packages: bokeh.plotting for visualization and numpy for mathematical operations

  • Generate x−values using np.arange() from 0 to 4? with step size 0.1

  • Calculate y−values using np.sin() function

  • Use output_file() to specify the HTML output file name

  • Create a figure object with title and axis labels using figure()

  • Add line renderer using p.line() with styling options

  • Display the interactive plot using show()

Key Features

  • Interactive plots: Users can zoom, pan, and hover over data points

  • HTML output: Plots are saved as standalone HTML files

  • Customization: Easy styling with colors, line width, and legends

  • Web integration: Can be embedded in Flask or Django applications

Conclusion

Bokeh provides an excellent way to create interactive sinusoidal wave visualizations. The combination of NumPy for mathematical computations and Bokeh for plotting creates dynamic, web−ready visualizations that enhance data understanding.

Updated on: 2026-03-25T14:59:35+05:30

488 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements