Lecture#06
Lecture#06
Plots in MATLAB
Topics Covered:
1. Plotting basic 2-D/3-D plots.
label 1000
Text
800
Tick-mark
INTENSITY (lux)
600
400
Data symbol
200
0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
x axis Tick-mark label
label
TWO-DIMENSIONAL plot() COMMAND
The basic 2-D plot command is:
plot(x,y)
vectors x and y.
Given data:
x 1 2 3 5 7 7.5 8 10
y 2 6.5 7 7 5.5 4 6 8
plot(x,y,’line specifiers’)
LINE SPECIFIERS IN THE plot() COMMAND
plot(x,y,‘line specifiers’)
The specifiers are optional. This means that none, one, two, or
all the three can be included in a command.
EXAMPLES:
plot(x,y) A solid blue line connects the points with no markers.
plot(x,y,’r’) A solid red line connects the points with no markers.
plot(x,y,’--y’) A yellow dashed line connects the points.
plot(x,y,’*’) The points are marked with * (no line between the
points.)
plot(x,y,’g:d’) A green dotted line connects the points which are
marked with diamond markers.
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot() COMMAND
Line Specifiers:
dashed red line and
asterisk markers.
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot() COMMAND
0.5 x
Consider: y 3.5 cos(6 x) for 2 x 4
A script file for plotting the function is:
If the vector x is created with large spacing, the graph is not accurate.
Below is the previous plot with spacing of 0.3.
x = [-2:0.3:4];
y = 3.5.^(-0.5*x).*cos(6*x);
plot(x,y)
THE fplot COMMAND
fplot(‘function’,limits)
The limits is a vector with the domain of x, and optionally with limits
of the y axis:
[xmin,xmax] or [xmin,xmax,ymin,ymax]
Line specifiers can be added.
PLOT OF A FUNCTION WITH THE fplot() COMMAND
plot(x,y,u,v,t,h)
plot(x,y,’-b’,u,v,’—r’,t,h,’g:’)
USING THE plot() COMMAND TO PLOT
MULTIPLE GRAPHS IN THE SAME PLOT
120
100
80
60
40
20
-20
-40
-2 -1 0 1 2 3 4
USING THE hold on, hold off, COMMANDS
TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
hold on Holds the current plot and all axis properties so that
subsequent plot commands add to the existing plot.
This method is useful when all the information (vectors) used for
the plotting is not available a the same time.
USING THE hold on, hold off, COMMANDS
TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
Plot of the function, y 3 x 3
26 x 10 and its first and second
derivatives, for 2 x 4 all in the same plot.
x = [-2:0.01:4];
y = 3*x.^3-26*x+6;
yd = 9*x.^2-26;
ydd = 18*x;
plot(x,y,'-b') First graph is created.
hold on
plot(x,yd,'--r')
Two more graphs are created.
plot(x,ydd,':k')
hold off
Example-02
Subplots
• subplot(m,n,p) : create a subplot in an array of axes
>> subplot(2,3,4);
m
P=4
n
Subplots Example 2
Subplots Example 2
Subplots Example 3
EXAMPLE OF A FORMATTED 2-D PLOT
Plot title
Light Intensity as a Function of Distance
1200 Legend
Theory
y axis Experiment
label 1000
Text
800
Tick-mark
INTENSITY (lux)
600
400
Data symbol
200
0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
x axis Tick-mark label
label
FORMATTING PLOTS
1. Formatting commands.
In this method commands, that make changes or additions to
the plot, are entered after the plot() command. This can be
done in the Command Window, or as part of a program in a
script file.
title(‘string’)
Adds the string as a title at the top of the plot.
xlabel(‘string’)
Adds the string as a label to the x-axis.
ylabel(‘string’)
Adds the string as a label to the y-axis.
legend(‘string1’,’string2’,’string3’)
Creates a legend using the strings to label various curves
(when several curves are in one plot). The location of the
legend is specified by the mouse.
text(x,y,’string’)
Places the string (text) on the plot at coordinate x,y relative to
the plot axes.
gtext(‘string’)
Places the string (text) on the plot. When the command
executes the figure window pops and the text location is clicked
with the mouse.
FORMATTING COMMANDS
hold on
plot(xd,yd,'ro--','linewidth',1.0,'markersize',10)
hold off
EXAMPLE OF A FORMATTED PLOT
plot the data x=( 3 4 6 8 10), y=( 12 14 16 20 22) ,using Bars, Stairs
and stem plots.