How to plot a signal

What is a signal?

The signal is defined as data values that are represented by the signal vector at specified points in time. Let’s create a signal plot in Matlab and then in Python.

MATLAB and Python are powerful programming languages for various purposes, including numerical and scientific analysis. Both of these languages can display the data graphically.

MATLAB code

We will create a Continuous Time (CT) signal, often used in analog systems. Let’s say we have a CT signal, s=2tcos(t)s=2t \cos\:(t) for a time interval (0–20). Here x(t)x(t) is a signal defined as a function of time tt. Let’s look at the code to understand it:

time=0:0.01:20;
signal=2*time.*cos(time);
# Now Plotting
h1=figure;
plot(time,signal,'color','r');
xlabel('Time');
ylabel('CT Signal');
title('Signal Plot');
grid;

Explanation

  • Line 1–2: We declare our axes i.e., time and signal.

  • Line 6: We plot the data values using the plot()function. An important thing to note here is that time is independent variable and signal is a dependent variable. We also specify the color of the plotted signal.

  • Line 9: We use the grid function to display the grid line on the plot.

Example

Let’s create a sinusoidal signal, which is an example of a continuous-time signal, to understand the concept better. For example, we have a CT signal, x(t)=sin(2πt)x(t) = \sin\:(2\pi{t}) for a time interval (0-10). Here x(t)x(t) is a signal defined as a function of time tt.

% time interval values
t = 0:0.01:10;
% the signal values
x = sin(2*pi*t);
% Plotting the signal
h1=figure;
plot(t, x, 'color', 'b')
xlabel('Time')
ylabel('CT signal')
title('Sinusoidal Signal')
grid;

Explanation

  • Line 2: We declare x-axis ie., time.

  • Line 5: We declare y-axis ie., signal.

  • Line 9: We plot the data values using the plot() function. We also specify the color of the plotted signal.

Python code

Let's try to plot the same signal in Python:

# Importing pyplot and numpy from matplotlib
from matplotlib import pyplot as plt
import numpy as np
# Setting the figure size
plt.rcParams['figure.figsize'] = (10,6)
# Defining variables
interval=0.01
time=np.arange(0,20,interval)
signal=2*time*np.cos(time)
# Plotting
plt.plot(time,signal,color="red")
plt.grid()
plt.margins(x=0)
plt.xlabel('Time')
plt.ylabel('CT Signal')
plt.title('Signal Plot');
plt.show()

Explanation

  • Line 2–3: Importing some important libraries required for data arrangement and plotting i.e., matplotlib and numpy.

  • Line 9–11: We define the variables i.e., time and signal.

  • Line 14: Plotting the graph by using plt.plot() function.

  • Line 20: The function plt.show() is used to display the plot.

Second signal

Let’s create a sinusoidal signal, which is an example of a continuous-time signal, to understand the concept better. For example, we have a CT signal, x(t)=sin(2πt)x(t) = \sin\:(2*\pi*t) for a time interval (0-10). Here x(t)x(t) is a signal defined as a function of time tt.

# Importing pyplot and numpy from matplotlib
from matplotlib import pyplot as plt
import numpy as np
# Setting the figure size
plt.rcParams['figure.figsize'] = (10,6)
# Defining variables
interval=0.01
time=np.arange(0,10,interval)
signal=np.sin(2*np.pi*time)
# Plotting
plt.plot(time,signal,color="red")
plt.grid()
plt.margins(x=0)
plt.xlabel('Time')
plt.ylabel('CT Signal')
plt.title('Signal Plot');
plt.show()

Explanation

  • Line 2–3: Importing some important libraries i.e., matplotlib and numpy.

  • Line 9–11: Defining axes i.e., time and signal.

  • Line 14: Plotting the graph by using plt.plot() function.

  • Line 20: The function plt.show() is used to display the plot.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved