Matplotlib - Time Plots and Signals



A time plot is a graph that represents the change of a variable over a certain time period. Similarly, signals represent the change in the amplitude of a wave over a time period. In a two-dimensional space, time is plotted on the horizontal axis, while the value is plotted on the vertical axis.

Imagine we are measuring the temperature every hour for a day. We plot the time on x-axis and the temperature on y-axis. Connecting the y-axis values through a line represents the time plot −

Time Plots and Signals

Time Plots and Signals in Matplotlib

Time plots and signals in Matplotlib refer to visual representations of changes in data values over a time period. They display how data points vary with respect to time.

Matplotlib does not have a separate function specifically for creating time plots or signals. Instead, we use the plot() function to generate these plots. This function accepts the X and Y coordinates as arrays and connects them using lines to create a time plot or signal

Lets start by drawing a basic time plot.

Basic Time Plot

A basic time plot in Matplotlib is a way to visualize changes in data over time. It involves plotting points on a graph where the x-axis represents time and the y-axis represents the values of the data. The plot is formed is formed by connecting the y-axis values as we progress along the x-axis.

Example

The following example creates a basic time plot in Matplotlib. We create two arrays: 'time' represents the time period, and 'values' represents the corresponding values collected at each time period.

Then, we use the plot() function to connect the different values on y-axis. This results in a plot that shows changes in values over time −

import matplotlib.pyplot as plt

# Sample time-series data
time = [0, 1, 2, 3, 4, 5]
values = [10, 15, 20, 18, 25, 30]

# Creating a time plot
plt.plot(time, values, marker='o')

# Adding labels and title
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Basic Time Plot')

# Displaying the plot
plt.show()

Output

Following is the output of the above code −

Basic Time Plot

Sine Wave Signal

A sine wave signal in Matplotlib represents a wave that varies sinusoidally over time. Time is plotted on the x-axis, while the amplitude of the wave is plotted on the y-axis. A sine wave signal looks like a smooth, wavy line when plotted on a graph, with peaks and troughs repeating at regular time intervals.

Example

In here, we are generating a sine wave signal to display the amplitude of a wave over a time period. First, we create an array of evenly spaced time intervals 't' ranging from the 0 to 10.

Then, we define the amplitude (height of the wave) and frequency (number of cycles made in a given time interval) of the wave. Using time, frequency and amplitude, we create a sine wave signal. The resultant plot displays a sine signal that repeats at regular intervals −

import matplotlib.pyplot as plt
import numpy as np

# Generating time values
t = np.linspace(0, 10, 100)

# Generating sine wave signal
frequency = 0.5
amplitude = 5
signal = amplitude * np.sin(2 * np.pi * frequency * t)

# Plotting the sine wave signal
plt.plot(t, signal)

# Adding labels and title
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Sine Wave Signal')

# Displaying the plot
plt.show()

Output

Output of the above code is as follows −

Sine Wave Signal

Random Time Plot

In Matplotlib, a random time plot is a visualization of a sequence of data points that change randomly over a time period. This is achieved by adding a random value to the previous data point, resulting in an irregular time plot.

Example

The following example creates a random time plot to display the random change in values over a time period. We start by creating a time interval ranging from 0 to 99.

Next, we generate random values for the y-axis by using the np.random.randn() function and calculate the cumulative sum at each time period using the np.cumsum() function. Then, we use the plot() function to generate the resultant plot where the data values are randomly plotted −

import matplotlib.pyplot as plt
import numpy as np

# Generating time values
t = np.arange(0, 100)

# Generating random walk time series
np.random.seed(5)
signal = np.cumsum(np.random.randn(100))

# Plotting the random walk time series
plt.plot(t, signal)

# Adding labels and title
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Random Time Plot')

# Displaying the plot
plt.show()

Output

After executing the above code, we get the following output −

Random Time Plot

Step Function Signal

A step function signal in Matplotlib defines a signal whose values change significantly at specific points in time. In a step function signal, horizontal lines are connected by vertical lines that indicate the moments when the value changes. Step function signals represent events that occur instantaneously.

Example

Now, we are creating a step function signal in Matplotlib to display sudden changes in values at a given time period. We start by defining a time interval ranging from 0 to 10. Then, we use the np.piecewise() function to create the condition for when the values on y-axis should change.

The condition we use is as follows: "The amplitude remains 0 until t reaches 4 seconds. Then, at t=4 seconds, the amplitude changes to 1 and remains 1 until time reaches 6 seconds. Finally, at t=6 seconds, the amplitude returns to 0" −

import matplotlib.pyplot as plt
import numpy as np

# Generating time values
t = np.linspace(0, 10, 100)

# Generating step function signal
signal = np.piecewise(t, [t < 4, (t >= 4) & (t < 6), t >= 6], [0, 1, 0])

# Plotting the step function signal
plt.plot(t, signal)

# Adding labels and title
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Step Function Signal')

# Displaying the plot
plt.show()

Output

The output obtained is as shown below −

Step Function Signal
Advertisements