No Slide Title

Download Report

Transcript No Slide Title

"A picture is worth a thousand words."
Graphical representation is useful for:
• Error detection - you can locate outliers in a
dataset, program bugs, monitor numerical error
growth.
• Analysis - explore the data, help find relationships
among variables, patterns, suggest new
hypotheses.
• Communication and presentation of results;
visualization of a concept.
1
Objectives
You should be able to:
 Recognize the various types of plots that
Matlab can create.
 Create simple 2D plots
 Add labels, title, and annotations to a plot.
 Graph several variables on the same plot or
produce multiple subplots on a page
 Save your plots in various formats or print.
2
Plotting and Graphics
One of MATLAB’s strengths is its graphing
capability. The user can specify
graph types - line, 2-D, 3-D, histogram,
surface, contour, etc.
color, line style, marker style
plot combinations (several plots on the
same axis or different axes but grouped
together)
3
Figure Windows
The Figure Window is designed to display
data. Unlike the command window, it cannot
accept text commands. You can explicitly open a
figure window by typing in the command window
>> figure(1)
1 is an index to this particular window.
To close this window, either 1) click on the square
(with an ) in the upper right corner or 2) type
>> delete(1)
4
X-Y Plotting
There are four types of X-Y plots, as shown
below. Here, plot is the “standard” way to plot
data. loglog provides a log scale on both the x
and y axis.
The arguments for all four
specifications are the same.
Graph Paper
plot
linear x-y plot
loglog
loglog x-y plot
semilogx semi-log x-y plot (x-axis logarithmic)
semilogy semi-log x-y plot (y-axis logarithmic)
5
Basic Plotting
One of the simplest plots can be achieved with a
single instruction, as follows.
>> Y = [0 .48
.85 1 .91
.6
.14];
>> plot(Y)
6
Basic Plotting (cont’d)
To add a title, axis labels, and a grid, type
>>title(‘Plot1’)
>>xlabel(‘The X-Axis’)
Semi-colons here
do nothing.
>>ylabel(‘The Y-Axis’)
>>grid
Semi-colons can
be used to end a
line,
as
can
comma or space.
7
Basic Plotting (cont’d)
Two vectors of the same length can be plotted
against each other using the plot(X,Y), where the
X vector is plotted on the abscissa (horizontal)
and the Y vector is plotted on the ordinate
(vertically).
>> t = 0: .05: 4*pi;
This produces a vector
of values from 0 to 4*pi
separated by .05.
>> y = sin(t);
>>plot(t,y)
This produces a vector
of values related by the
sin function to t.
8
Basic Plotting (cont’d)
Curve ends
at 4*pi.
9
Basic Plotting (cont’d)
Note that MATLAB scales the axes so that
the data fits within the boundaries. The tick
marks are placed evenly on the axis, and
extra space is placed at the end when data
does not fall on a tick mark (observe the
right side of the plot). The number of tick
marks can be changed by changing the size of
the window – a smaller window produces
fewer tick marks.
10
Basic Plotting (cont’d)
More than one curve can be plotted on the same
plot, using multiple arguments in the plot
command. For example, consider
>>f = 0:10; w = 2*f; v = .5*f.^2;
>> plot(f,w,f,v)
f is repeated.
w is a linear function of f,
and v is a quadratic
function of f. Here, both
are plotted verses f.
11
Basic Plotting (cont’d)
Notice that MATLAB
chooses different line
colors.
12
Basic Plotting (cont’d)
In this last example, two functions (vectors) of y
were plotted against the same function (vector)
of x. That is, the vector associated with the xaxis is the same for both functions. However,
different vectors may be chosen. It is important
to note that, for every x-y pair, the vectors must
be the same length.
13
Basic Plotting (cont’d)
If you ask MATLAB to plot data (using plot)
specified as a 2-dimensional matrix, it will plot
each column as a separate curve. On the other
hand, a row vector is converted into a column
vector, and the resulting sequence of values is
plotted. You can plot a row vector against a
column vector, as long as they both have the
same dimensions.
14
Basic Plotting (cont’d)
If you specify plot(Y), where Y is a 2dimensional matrix, then MATLAB will plot
each column in Y as a separate curve verses its
position in the matrix. If you specify plot(x,Y),
where x is a vector, then MATLAB will plot each
column in Y as a separate curve versus x.
>> x = (-5: 0.2 : 5)’;
Necessary so Y can be
represented as 3 functions.
>>Y = [x .5*x.^2 (x.^3)/6];
15
Basic Plotting (cont’d)
>> plot(Y); grid
Applies gridlines.
Note that a ‘;’
separates
the
two commands.
A comma would
also work.
16
Basic Plotting (cont’d)
>> plot(x,Y), grid
Note change
in x-axis.
17
Basic Plotting (cont’d)
>> plot(x, Y), grid
is an abbreviated version of
>> plot(x, Y(:,1), x, Y(:,2), x, Y(:,3)), grid
The latter may be useful if you want to plot
certain columns but not others.
18
Line and Mark Styles
You can specify the line type, the data mark,
and colors of curves. Line types and data marks
are shown below.
Line Types
Solid
Dashed
-Dotted
:
Dashdot
-.
Point Types
Point
.
Plus
+
Star
*
Circle
o
x –mark
x
19
Line and Mark Styles (cont’d)
>>x = 1:10;
>>w = 2*x - 1;
>>plot(x,w,’x’)
Notice that the
line is missing.
20
Line and Mark Styles (cont’d)
Notice that MATLAB forms a plot by connecting
data marks with straight lines.
Therefore,
smoother plots are achieved with more data
points.
Line colors can be be chosen using letters,
as shown on the next slide. You can also access
any of 16 colors using the format.
>>plot(x,Y,’c#’)
where # is an integer between 0 and 15. Thus,
>>plot(x, Y, ‘r’)
produces a solid red line, while
21
Line and Mark Styles (cont’d)
>>plot(x, Y, ‘+g’)
produces green ‘+’ symbols.
The order of the two
symbols + and g is not
important.
+g is
equivalent to g+.
Colors
Yellow
Magenta
Cyan
Red
Green
Blue
White
Black
Invisible
y
m
c
r
g
b
w
k
i
22
Complex Data Representation
When arguments of the plot command are complex,
only the real part is plotted. If there is only one
complex argument, plot produces a plot of the real
verses the imaginary part.
>> t = linspace(1,10, 50); x = linspace(0, 6*pi, 50);
>> z = exp(-t).*(sin(x) + 1i*cos(x));
>> plot(z)
Recall: 1i assures that
MATLAB views i as
>> xlabel(‘Real Part’)
1
regardless of a
>> ylabel(‘Imaginary Part’) prior redefinition; e.g. i =
2.
23
Complex Data Representation (cont’d)
24
Polar Plots
The polar command allows you to plot in polar
coordinates. For example, you can plot antenna
radiation patterns. You invoke the polar command
with
polar(theta,rho)
where the angle vector is theta,and the magnitude
vector is rho. You can plot a complex number as
>>t=linspace(1,10,50); x=linspace(0,6*pi,50);
>>z = exp(-t).*(sin(x) + 1i*cos(x));
>>polar(angle(z),abs(z))
25
Polar Plots (cont’d)
26
Plot Annotation and Legends (cont’d)
MATLAB allows you to mark a special data point,
such as a maximum value. So, for example,
Here, m returns the max
>>[m,i] = max(z);
value of z, while, i is its index.
>>text(real(z(i)), imag(z(i)), ’Max_Value’)
inserts ‘Max_Value’, when this is applied after
>> t=linspace(1,10,50); x=linspace(0, 6*pi, 50);
>> z=exp(-t).*(sin(x)+1i*cos(x));
>> plot(z)
27
Figure Window Control
MATLAB can plot two or more graphs in
the same Figure Window using the subplot
command. Specifically, subplot(m,n,p), specifies
the Figure Window to be divided into m rows, n
columns, with the current plot indexed by p. For
example, we can plot two plots one above the
other with
>>x = linspace(0, 2*pi,20);
>>subplot(2,1,1), plot(x)
>>subplot(2,1,2), plot(-2*x)
28
Figure Window Control (cont’d)
29
Figure Window Control (cont’d)
Suppose you want to produce several figures in one
program. You can either create the first figure and then
clear it with:
>> clf
Or, you may just open a new figure window:
>> figure(2)
If you want to go back to working on figure (1), simply
type
>> figure(1)
And commands entered after that will apply to figure 1.
30
Figure Window Control (cont’d)
>>subplot(1,1,1)
>>subplot(2,2,1), plot(x)
>>subplot(2,2,2), plot(-2*x)
>>subplot(2,2,3), plot(sin(x))
>>subplot(2,2,4), plot(cos(2*x))
>>subplot(2,2,3), title(‘Plot’), grid
produces the figure on the next slide. Note the
title specification at the end.
Note that anything you type here like xlabel(‘x axis’) affects
only subplot(2,2,3).
31
Figure Window Control (cont’d)
32
Figure Window Control (cont’d)
A useful MATLAB command is the hold
command. This prevents MATLAB from writing
over a previously established Figure Window
with a newly specified one. By typing hold on,
you can plot a new curve on the same graph as
previously plotted curves. To revert back to the
default condition, type hold off. In this case,
new plot function replaces the old Figure
Window, erasing previous plots. Typing hold
just causes the hold on and hold off to toggle
back and forth.
33
Manual Axis Scaling
MATLAB allows you to override the automatic axis
scaling features and to set the plotting limits.
1. axis([XMIN, XMAX, YMIN,YMAX]) - sets axis limits for 2D plots.
2. axis(‘auto’) – returns axis to autoscaling mode (antidote for
axis(axis).
3. axis(‘equal’) – sets the axis so that tick marks on the x and
y axis are equal.
4. axis(‘normal) – sets the axis back to unequal tick marks
(antidote for axis(‘equal’).
5. axis(‘off’) - removes the axis, tick marks, and numbers
34
from the plot.
Manual Axis Scaling (cont’d)
Example:
You can force the axis of some figure to be
the same as some other figure, as follows.
>>figure(1)
>>A = axis;
>>figure(2)
Without
the
“;”,
MATLAB would print
out the axis limits horizontal and vertical.
>>axis(A)
This can also be used with subplot functions.
35
Hardcopy
You can obtain a hardcopy of the Figure Window
by clicking on the File and then Print at the
Figure Window. In the window that appears,
choose which printer you want and click OK.
Alternatively, you can just type print in the
command window.
This will cause your
Figure Window to print to the default printer.
36
Saving and exporting figures
You can choose to save the plot in another format
such as jpg, tif, eps, or bmp by using “Export”
from the File menu in the Figure window.
For plots that you want to save in Matlab and
annotate later, you can “Save as” a .fig file.
37