MATLAB Two-Dimensional Plotting: From Data to Axes

MATLAB Two-Dimensional Plotting turns paired numerical values into a graph you can inspect. In engineering, you might plot position against time or force against displacement. A useful graph preserves the pairing of measurements, identifies the quantities and units, and makes clear whether you are showing individual samples or a connected trend.

Map values to coordinates

For real-valued vectors, the governing coordinate rule is


plot(x,y):Pi=(xi,yi),i=1,2,…,n.\texttt{plot(x,y)}:\qquad P_i=(x_i,y_i),\quad i=1,2,\ldots,n.


The first input supplies horizontal coordinates; the second supplies vertical coordinates. A coordinate pair consists of the corresponding entries from these two vectors. Each horizontal value needs a vertical partner, so the vectors must contain the same number of entries. Either vector may be a row or column vector.


For example, a cart has measured positions at three times:


MATLAB
x = [0 2 4];       % time in seconds
y = [1 3 2];       % position in metres
plot(x,y,'-o')


The plotted coordinates are (0,1)(0,1), (2,3)(2,3), and (4,2)(4,2), with time in seconds and position in metres. MATLAB connects them in their stored order; it does not sort the horizontal values first.


Three position measurements are connected in increasing time order.


Writing plot(y) instead uses horizontal coordinates 1,2,31,2,3. Those are sample indices, not the supplied measurement times. Use explicit horizontal coordinates whenever their values carry physical meaning.

Choose what the graph communicates

A line specification is a compact character sequence that selects a colour, line style, or marker. For example, 'b--o' requests a blue dashed line with circle markers.


Command

Appearance

Useful interpretation

plot(x,y)

Connected solid line

Emphasizes change between samples

plot(x,y,'o')

Circle markers only

Emphasizes individual measurements

plot(x,y,'-o')

Solid line and circles

Shows samples and their connections

plot(x,y,'--')

Dashed line

Distinguishes a comparison series


A connecting segment represents the straight path between adjacent plotted points. It does not establish what the measured system actually did between samples. A smooth-looking curve requires sufficiently close samples of the underlying relationship.



Label both axes with quantities and units. A title gives context, while a grid helps you compare coordinates:


MATLAB
xlabel('Time (s)')
ylabel('Position (m)')
title('Cart position')
grid on


These commands describe the current axes; they do not convert the numerical data into different units.

Compare series on the same axes

You can supply several coordinate pairs in one call:


MATLAB
plot(x,yMeasured,'o',x,yReference,'--')
legend('Measured','Reference')


Here the legend labels follow the order of the plotted series. Each series needs matching horizontal and vertical vector lengths, but separate series may contain different numbers of points.


Alternatively, add series with separate calls:


MATLAB
figure
plot(x,yMeasured,'o')
hold on
plot(x,yReference,'--')
hold off
legend('Measured','Reference')


figure opens a new figure. With the default hold state off, a new plot call replaces existing plots in the current axes. hold on retains them while you add another series. hold off restores replacement behaviour for subsequent plotting calls; it does not immediately remove existing series.



What to remember

  • plot(x,y) pairs corresponding entries and connects them in stored order.
  • Supply physical horizontal coordinates explicitly.
  • Select markers and lines to communicate what the data support.
  • Label quantities and units, and identify comparison series with a legend.
  • Use hold on before adding a series with another plotting call.

Example: Compare a Force Ramp with Its Reference

A loading rig is intended to increase force according to Fref=(12 N/s)tF_{\text{ref}}=(12\ \text{N/s})t. A sensor records the following measurements:


Time (s)

Measured force (N)

0

0

1

12

2

25

3

37


a) Identify the plotted coordinate pairs and write MATLAB code to display the measured forces as circle markers without connecting lines.


Time belongs on the horizontal axis and force on the vertical axis. Reading across each table row gives the coordinate pairs (0,0)(0,0), (1,12)(1,12), (2,25)(2,25), and (3,37)(3,37), with time in seconds and force in newtons.


Store the entries in matching order. The specification 'o' requests circle markers without a connecting line:


MATLAB
t = [0 1 2 3];
Fmeas = [0 12 25 37];
figure
plot(t,Fmeas,'o')


Both vectors contain ________ entries, so each time has exactly ________ corresponding measured force. The final plotted measurement is at t=3 st=3\ \text{s} and F=37 NF=37\ \text{N}.


b) Calculate the reference forces at the recorded times. Add the reference as a dashed line to the same axes, and include axis labels, a title, a legend, and a grid.


Evaluate the reference relationship at each recorded time:


Fref(0 s)=(12 N/s)(0 s)=0 N,Fref(1 s)=(12 N/s)(1 s)=12 N,Fref(2 s)=(12 N/s)(2 s)=24 N,Fref(3 s)=(12 N/s)(3 s)=36 N.\begin{aligned} F_{\text{ref}}(0\ \text{s})&=(12\ \text{N/s})(0\ \text{s})=0\ \text{N},\\ F_{\text{ref}}(1\ \text{s})&=(12\ \text{N/s})(1\ \text{s})=12\ \text{N},\\ F_{\text{ref}}(2\ \text{s})&=(12\ \text{N/s})(2\ \text{s})=24\ \text{N},\\ F_{\text{ref}}(3\ \text{s})&=(12\ \text{N/s})(3\ \text{s})=36\ \text{N}. \end{aligned}


Thus, the reference vector is [0 12 24 36] in newtons. Continue after the commands in part a), turning hold on before the second plotting call so that the measured markers remain:


MATLAB
Fref = 12*t;
hold on
plot(t,Fref,'--')
hold off
xlabel('Time (s)')
ylabel('Force (N)')
title('Loading rig force ramp')
legend('Measured','Reference')
grid on


The legend follows creation order: measured markers first, reference line second. Because the reference relationship is linear, connecting its sampled values with straight segments also displays that relationship between the recorded times.


Measured force markers and a dashed reference line share time and force axes.


c) Determine the largest absolute difference between measured and reference force at the recorded times. State every recorded time at which it occurs.


Compare forces at the same times. Define the signed difference as measured force minus reference force, then take its absolute value:


ΔF=Fmeas−Fref=[0−0, 12−12, 25−24, 37−36] N=[0,0,1,1] N,∣ΔF∣=[0,0,1,1] N.\begin{aligned} \Delta F&=F_{\text{meas}}-F_{\text{ref}}\\ &=[0-0,\ 12-12,\ 25-24,\ 37-36]\ \text{N}\\ &=[0,0,1,1]\ \text{N},\\ |\Delta F|&=[0,0,1,1]\ \text{N}. \end{aligned}


The largest absolute difference is 1.0 N, occurring at both 2 s and 3 s. At those times the measured force exceeds the reference. This comparison establishes the largest difference among the recorded samples; it does not establish the largest physical difference between measurements.

A load sensor records force at four times:


MATLAB
t = [0 0.4 0.8 1.2];   % seconds
F = [0 8 15 21];        % newtons


Which command plots force vertically against the recorded times horizontally, using circle markers with no connecting line?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An engineer uses the following MATLAB commands to display temperature measurements taken during a cooling test. Complete the description using the full English names of the colour, line style and marker shape.


MATLAB
t = [0 3 6 9];
T = [72 61 53 47];
plot(t,T,'k--s')
xlabel('Time (s)')
ylabel('Temperature (degrees C)')


The curve is , consecutive measurements are connected by a line, and each measurement has a marker.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An engineer compares three conveyor speed records. Time is in seconds and speed is in metres per second. Starting with a new figure, the engineer runs:


MATLAB
t = [0 1 2];
vA = [0 2 4];
vB = [0 3 6];
vC = [0 4 8];
figure
plot(t,vA,'-o')
hold on
plot(t,vB,'--s')
hold off
plot(t,vC,':d')


Which speed records remain visible after every command has executed?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice