What is difference between stem and plot in MATLAB?

MATLAB Illustration

In MATLAB, both the plot and stem functions are used to visualize data curves, but they represent data in fundamentally different formats:

  • plot function: Displays continuous data values by connecting data points with straight line segments. Think of drawing the graph of y = sin(x) smoothly with a pencil without lifting it from the paper.
  • stem function: Displays discrete data values by drawing vertical stems extending from the baseline (x-axis) to each data point with a marker at the top. Think of plotting y = sin(x) for specific angles (such as 0°, 15°, 30°, 45°, 60°, and 90°) and drawing individual vertical lines from the x-axis to each point.

MATLAB Code Example: Continuous vs. Discrete Visualization

Run the following MATLAB code to compare plot and stem side-by-side using subplots:

MATLAB
clc;
clear all;
close all;
% Generate 50 points linearly spaced between -pi and pi x = linspace(-pi, pi, 50);
y = sin(x);
% Subplot 1: Continuous representation using 
plot() sub
plot(1, 2, 1);
plot(x, y, 'b-', 'LineWidth', 1.5);
xticks([-pi 0 pi]);
xticklabels({'-pi', '0', 'pi'});
title('Continuous Plot: 
plot(x, y)');
xlabel('x (radians)');
ylabel('sin(x)');
grid on;
% Subplot 2: Discrete representation using stem() sub
plot(1, 2, 2);
stem(x, y, 'r', 'LineWidth', 1.2, 'MarkerFaceColor', 'r');
xticks([-pi 0 pi]);
xticklabels({'-pi', '0', 'pi'});
title('Discrete Stem Plot: stem(x, y)');
xlabel('x (radians)');
ylabel('sin(x)');
grid on;

Code Explanation

  • linspace(-pi, pi, 50) creates a vector of 50 evenly spaced points from to π.
  • subplot(1, 2, 1) and subplot(1, 2, 2) divide the figure window into two side-by-side panels.
  • xticks and xticklabels format the x-axis tick marks using LaTeX notation (, 0, π).
  • grid on adds a background grid for better visual clarity.

Conclusion: The plot panel presents a smooth, continuous waveform, whereas the stem panel highlights individual discrete sample points rooted at the baseline.

Verified Feedback

What Engineering Students Say

Real feedback from students across top engineering universities worldwide.

Verified Student

“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”

AS

Aditi Sharma

IIT Bombay • Signal Processing Coursework
Verified Student

“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”

JM

John M.

Monash University, Australia • Simulink Dynamic Model
Technical Knowledge Base

Latest MATLAB Guides & Tutorials

Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.

MATLAB Guide 5 Min Read

Physics-Informed Neural Networks (PINNs) for Microgrid Dynamics and Power Flow in MATLAB

Modern microgrids operate with low physical inertia, rapid inverter switching dynamics, and intermittent renewable power generation. Simulating these systems requir...

MATLAB Guide 5 Min Read

ROS 2 and MATLAB Co-Simulation for Autonomous Mobile Robot Path Tracking Using NMPC

Tracking complex trajectories with non-holonomic mobile robots requires handling physical constraints such as actuator saturation, wheel slip, and sharp cornering. Standard cont...