Two Dimensional Plots
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
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:
x = [0 2 4]; % time in seconds
y = [1 3 2]; % position in metres
plot(x,y,'-o')The plotted coordinates are (0,1), (2,3), and (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.
Writing plot(y) instead uses horizontal coordinates 1,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:
xlabel('Time (s)')
ylabel('Position (m)')
title('Cart position')
grid onThese 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:
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:
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)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), (1,12), (2,25), and (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:
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 s and F=37 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:
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:
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 onThe 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.
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:
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.
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.
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.
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:
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?