how to plot a two signal on same axes?

H
hardikisharma2 · Sep 12, 2020 · 1.9K views
Question
i am generating two signal and i want to plot on same axes.
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Sep 9, 2026





   

Plot Two Signals on the Same Axes in MATLAB

   

Method 1: Using hold on (Standard Approach)


   

Use hold on to retain the current plot when adding subsequent signals. This method works well when both signals share similar units and scales.

   

% Time vector and signals
t = linspace(0, 1, 1000);
signal1 = sin(2 * pi * 5 * t);          % 5 Hz sine wave
signal2 = 0.5 * cos(2 * pi * 10 * t);   % 10 Hz cosine wave

figure('Color', 'w');

% Plot first signal
plot(t, signal1, 'b-', 'LineWidth', 1.5);
hold on;

% Plot second signal
plot(t, signal2, 'r--', 'LineWidth', 1.5);
hold off;

% Add formatting
grid on;
xlabel('Time (s)');
ylabel('Amplitude (V)');
title('Two Signals on the Same Axes');
legend('Signal 1 (5 Hz Sine)', 'Signal 2 (10 Hz Cosine)', 'Location', 'northeast');

   

Method 2: Multiple Inputs in a Single plot() Command


   

Pass coordinate pairs directly into one plot() call.

   

t = linspace(0, 2*pi, 200);
y1 = sin(t);
y2 = cos(t);

figure('Color', 'w');
plot(t, y1, 'b-', t, y2, 'm-.', 'LineWidth', 1.8);
grid on;
xlabel('Angle (rad)');
ylabel('Value');
legend('sin(t)', 'cos(t)');

   

Method 3: Two Signals with Different Scales (yyaxis)


   

When signals have different units or magnitudes (for example, voltage and current), use yyaxis to create independent left and right y-axes on the same figure.

   

t = linspace(0, 0.05, 500);
voltage = 230 * sin(2 * pi * 50 * t);   % Large magnitude (Volts)
current = 2.5 * sin(2 * pi * 50 * t - pi/6); % Small magnitude (Amperes)

figure('Color', 'w');

% Left Y-axis for Voltage
yyaxis left
plot(t, voltage, 'b-', 'LineWidth', 1.5);
ylabel('Voltage (V)');
ylim([-300 300]);

% Right Y-axis for Current
yyaxis right
plot(t, current, 'r--', 'LineWidth', 1.5);
ylabel('Current (A)');
ylim([-4 4]);

% Shared formatting
xlabel('Time (s)');
title('Voltage and Current on Dual Y-Axes');
grid on;
legend('Voltage', 'Current', 'Location', 'northeast');

   

Method 4: Matrix Input (Column-Wise Plotting)


   

If signals share the exact same time vector and dimensions, combine them into matrix columns.

   

t = (0:0.001:1)';
Y = [sin(2*pi*3*t), cos(2*pi*3*t)]; % 1001-by-2 matrix

figure('Color', 'w');
plot(t, Y, 'LineWidth', 1.5);
grid on;
xlabel('Time (s)');
ylabel('Amplitude');
legend('Channel 1', 'Channel 2');

   


        Which Method to Choose:
       

               
  • Same units and range: Method 1 (hold on) or Method 2 (multiple inputs).

  •            
  • Different units or wide range difference: Method 3 (yyaxis).

  •            
  • Multichannel / array data: Method 4 (matrix input).

  •        

   


100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!