MATLAB - Lecture 22A: Two Dimensional Plots / Chapter 5
MATLAB - Lecture 22A: Two Dimensional Plots / Chapter 5
105
MAKING X-Y PLOTS
MATLAB has many functions and commands that can be used to
create various types of plots.
Plot title
1200
106
Legend
y axis label
1000
Text
800
Tick-mark
INTENSITY (lux)
400
Data symbol
200
10
12
14
16 DISTANCE (cm)
18
20
22
24
x axis label
Tick-mark label
106110
plot(x,y)
where x is a vector (one dimensional array), and y is a vector. Both vectors must have the same number of elements. The plot command creates a single curve with the the abscissa (horizontal axis) and the (vertical axis).
x values on
The curve is made from segments of lines that connect the points that are defined by the x and y coordinates of the elements in the two vectors.
106110
If data is given, the information is entered as the elements of the vectors x and y. If the values of
y are determined by a function from the values of x, than a vector x is created first, and then the values of y are calculated for each value of x. The spacing (difference) between the elements of x must be such that the plotted curve
106107
A plot can be created by the commands shown below. This can be done in the Command Window, or by writing and then running a script file. >> x=[1 2 3 5 7 7.5 8 10]; >> y=[2 6.5 7 7 5.5 4 6 8]; >> plot(x,y) Once the plot command is executed, the Figure Window opens with the following plot.
106107
106107
plot(x,y,line specifiers)
106107
plot(x,y,line specifiers)
Specifier
Specifier
Marker Specifier Type plus sign circle asterisk point square diamond + o * . s d
: --.
r g b c m y k
107108
The specifiers are typed inside the plot() command as strings. Within the string the specifiers can be typed in any order. The specifiers are optional. This means that none, one, two, or all the three can be included in a command. EXAMPLES:
plot(x,y)
plot(x,y,r) plot(x,y,--y)
plot(x,y,*)
plot(x,y,g:d)
The points are marked with * (no line between the points.)
A green dotted line connects the points which are marked with diamond markers.
110111
>> year = [1988:1:1994]; >> sales = [127, 130, 136, 145, 158, 178, 211]; >> plot(year,sales,'--r*')
110111
111112
y 3.5
0.5 x
cos(6 x)
for
2 x 4
y = 3.5.^(-0.5*x).*cos(6*x);
plot(x,y)
Once the plot command is executed, the Figure Window opens with the following plot.
A PLOT OF A FUNCTION
111112
y 3.50.5 x cos(6 x)
for
2 x 4
111112
If the vector x is created with large spacing, the graph is not accurate.
112113
= f(x)
fplot(function,limits)
The function is typed in as a string. 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]
112113
y x 2 4 sin(2 x) 1
for
3 x 3
114116
USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
114115
plot(x,y,u,v,t,h)
Plots three graphs in the same plot:
y versus x, v versus u,
Additional curves can be added.
and h versus t.
The curves can have a specific style by adding specifiers after each pair, for example:
plot(x,y,-b,u,v,r,t,h,g:)
USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
Plot of the function, derivatives, for x = [-2:0.01:4]; y = 3*x.^3-26*x+6;
114115
yd = 9*x.^2-26;
ydd = 18*x;
plot(x,y,'-b',x,yd,'--r',x,ydd,':k') Create three graphs, y vs. x (solid blue line), yd vs. x (dashed red line), and ydd vs. x (dotted black line) in the same figure.
USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
120 100 80 60 40 20 0 -20 -40 -2
114115
-1
This method is useful when all the information (vectors) used for the plotting is not available a the same time.
x = [-2:0.01:4]; y = 3*x.^3-26*x+6; yd = 9*x.^2-26; ydd = 18*x; plot(x,y,'-b') hold on First graph is created.
plot(x,yd,'--r')
plot(x,ydd,':k') hold off
106
Legend
y axis label
Text
800
Tick-mark
INTENSITY (lux)
400
Data symbol
200
10
12
14
16 18 DISTANCE (cm)
20
22
24
x axis label
Tick-mark label
FORMATTING PLOTS
A plot can be formatted to have a required appearance.
116122
FORMATTING PLOTS
There are two methods to format a plot:
116122
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. 2. Formatting the plot interactively in the Figure Window. In this method the plot is formatted by clicking on the plot and using the menu to make changes or add details.
FORMATTING COMMANDS
title(string)
Adds the string as a title at the top of the plot.
116122
xlabel(string)
Adds the string as a label to the x-axis.
ylabel(string)
Adds the string as a label to the y-axis.
116122
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.
120121
Creating vector x for plotting the theoretical curve. Creating vector y for plotting the theoretical curve. Creating a vector with coordinates of data points. Creating a vector with light intensity from data.
120121
axis([8 24 0 1200])
text(14,700,'Comparison between theory and experiment.','EdgeColor','r','LineWidth',2)
legend('Theory','Experiment',0)
120121