MATLAB Syntax & Technical Reference

What is difference between stem and plot in MATLAB?

Vectorized implementations, execution benchmarks, and step-by-step programming documentation.

spec_bench.m
Verified R2026a
Runtime: Base MATLAB / Online
Optimization: Vectorized O(1) / O(N)
Code Status: 100% Tested Script
Shortcut: Press F5 to Run

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.

Need Expert Guidance with MATLAB or Simulink?

Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with comprehensive code quality verification.

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 UAV Wind-Gust Rejection & Aerodynamic Modeling in MATLAB

Drones flying between city buildings, around offshore turbines, or over ridges run into sudden wind gusts and wake turbulence that linear controllers handle poorly. A tuned PID ...