Lecture 9
Plotting in 2-D
Plotting in 2-D, Plotting Multiple Curves,
Plotting with Figures, Plot Settings, Scaling,
Legends
Usefulness of plotting data
Large sets of data are usually difficult to
interpret as tables of numbers.
Engineers and scientists use graphical
techniques to reduce large sets of data to
help gain insight.
With graphical presentations, insightful
trends and possible errors are more easily
identified.
2-D, (x, y) plots
Creating (x, y) plots is easy with plot, e.g.:
Type two vectors: 100
90
x = 0:1:10;
80
y = x.^2; 70
Use the plot() 60
50
command: 40
plot(x,y); 30
20
10
0
0 1 2 3 4 5 6 7 8 9 10
Add a grid
Use the grid command to add a grid to the
figure. 100
grid on; 90
80
70
60
50
40
30
20
10
0
0 1 2 3 4 5 6 7 8 9 10
Add a title and labels
Add a title and labels to a plot as
illustrated in the example:
100
y vs. x
title('y vs. x'); 90
80
xlabel('x');
70
ylabel('y'); 60
y 50
40
30
20
10
0
0 1 2 3 4 5 6 7 8 9 10
x
Plot multiple curves on a figure
x = 1:1:10;
y1 = x.^2; 1000
900
y2 = x.^3; 800
plot(x,y1,x,y2); 700
600
500
400
300
200
100
0
1 2 3 4 5 6 7 8 9 10
The hold command
Multiple plots can be added to a figure using
the hold command.
1000
100
plot(x,y1); 900
90
800
80
hold on; 700
70
plot(x,y2); 600
60
… 500
50
400
40
hold off;
300
30
200
20
100
10
0
1 2 3 4 5 6 7 8 9 10
Multiple figures
Single (or multiple) plots can be created in
multiple figures. A two-figures example:
figure(1);
plot(x,y1);
figure(2);
plot(x,y2);
Plots of complex numbers
If input to the plot function is complex, MATLAB
plots the real component on the x-axis and the
imaginary on the y-axis. Alternatively, if
10
A = [1 + 1i, 1 + 2i,
2 + 3i, 3 + 4i]; 9
8
B = sin(A);
7
plot(A,B); 6
Note: MATLAB gives a 5
warning that imaginary 4
parts are ignored. 3
1
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
Changing line style, point style, and
color
x = 1:1:10;
y = [4, 6.2, 8, 3.2, 11, 7.6, 5.4, 2.1, 9.3, 4];
11
To plot the data with 10
a red, dash-dot line 9
with red stars for 8
points: 7
plot(x,y,'r-.*'); 6
2
1 2 3 4 5 6 7 8 9 10
Table of options
Line type Indicator Point type Indicator Color Indicator
solid - point . blue b
dotted : circle o green g
dash-dot -. x-mark x red r
dashed -- plus + cyan c
star * magenta m
square s yellow y
diamond d black k
triangle down v
triangle up ^
triangle left <
triangle right >
pentagram p
hexagram h
Plotting individual data points
Setting a marker type without specifying a line type will
suppress the straight line drawn by default between the
points that define the lines.
10
9
x = 1:1:10
8
y = 10:-1:1
7
plot(x,y)
6
plot(x,y,'o')
5
1
1 2 3 4 5 6 7 8 9 10
Re-scaling the axis
x = -3:1:10;
y = x.^2 + x;
plot(x,y);
120
If you do not like 100
100
the fact that the curve 80
touches the axis, you 80
60
can change the scale 60
as follows: 40
40
20
axis([-5, 12, -5, 112]); 20
0
0
-4 -4 -2 -2 00 22 4 4 6 6 8 810 10
12
Annotations with figures
x = 1:1:10;
y = x.^2;
100
plot(x,y); 90 Add text to plots with the text command.
text(2,90,'Add text to 80
plots with the 70
text command.'); 60
50
40
30
20
10
0
1 2 3 4 5 6 7 8 9 10
Addition of legends
x = 1:1:10;
y1 = x.^3 - x.^2;
1000
y2 = (-1*x).^3 + x.^2; 800
Line 1
Line 2
plot(x,y1,x,y2); 600
400
legend('Line 1', 'Line
200
2');
0
-200
-400
-600
-800
-1000
1 2 3 4 5 6 7 8 9 10
Special characters in legends
theta = -pi:0.01:pi;
y = sin(theta);
plot(theta,y); 1
y=sin()
0.8
legend('y = sin(\theta)');
0.6
Use the help feature to 0.4
search for how to input 0.2
0
other special characters.
-0.2
Search for 'text properties.' -0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
Exercises
In one figure, plot y1 = x2 - 2x + 3 and
y2 = -x3 + 3x + 1/x for x = -10 to x = 10. Make
y1 a solid line with green diamond points and y2
a dashed line with cyan x-mark points. Add a
legend with both curves listed in it.
In a second figure, plot y1 = tan(x) and y2 =
sin(x)^2 from x = -π to x = π. Scale the axes
appropriately and add a grid.
Summary
Plotting in 2-D
Adding a grid, title, labels, text, and a
legend to a plot
Scaling the axis on a plot
Plotting multiple curves on a figure
Plotting using multiple figures
Plotting complex arrays
Changing line style, color, and point style