Plotting a Sawtooth Wave using Matplotlib
Prerequisites:
A sawtooth waveform is a non-sinusoidal waveform because its teeth look like a saw. In an inverse (or reverse) sawtooth waveform the wave suddenly ramps downwards and then rises sharply. With Matplotlib we can draw different types of Graphical data. In this article, we will try to understand, How can we plot sawtooth waves using the Scipy python module.
Approach:
- Import required module.
- Create a sample rate. The NumPy linspace function is a tool in Python for creating numeric sequences that return evenly spaced numbers over a specified interval.
Syntax:
numpy.linspace(start, stop, num = 50,
endpoint = True, retstep = False, dtype = None)
- Plot a sawtooth wave.
- Label the graph.
- Display Graph.
Function
Syntax:
scipy.signal.sawtooth(t)
Parameter:
- t: The input time array.
Return:
Output array containing the sawtooth waveform.
Program:
Python3
from scipy import signalimport matplotlib.pyplot as plotimport numpy as np t = np.linspace(0, 1, 1000, endpoint=True) # Plot the sawtooth waveplot.plot(t, signal.sawtooth(2 * np.pi * 5 * t)) # Give x, y, title axis labelplot.xlabel('Time')plot.ylabel('Amplitude')plot.title('Sawtooth Signal - Geeksforgeeks') plot.axhline(y=0, color='k') # Displayplot.show() |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


