1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection -...

37
1 "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.

Transcript of 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection -...

Page 1: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

1

"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.

Page 2: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

2

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.

Objectives

You should be able to:

Page 3: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

3

graph types - line, 2-D, 3-D, histogram, surface, contour, etc.

color, line style, marker styleplot combinations (several plots on the

same axis or different axes but grouped together)

Plotting and GraphicsOne of MATLAB’s strengths is its graphing capability. The user can specify

Page 4: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

4

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)

To close this window, either 1) click on the square (with an ) in the upper right corner or 2) type

>> delete(1)

1 is an index to this particular window.

Page 5: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

5

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)

Page 6: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

6

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)

Page 7: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

7

Basic Plotting (cont’d)To add a title, axis labels, and a grid, type

>>title(‘Plot1’)

>>xlabel(‘The X-Axis’)

>>ylabel(‘The Y-Axis’)

>>grid

Semi-colons here do nothing.

Semi-colons can be used to end a line, as can comma or space.

Page 8: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

8

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;

>> y = sin(t);

>>plot(t,y)

This produces a vector of values from 0 to 4*pi separated by .05.

This produces a vector of values related by the sin function to t.

Page 9: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

9

Basic Plotting (cont’d)

Curve ends at 4*pi.

Page 10: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

10

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.

Page 11: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

11

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)

w is a linear function of f, and v is a quadratic function of f. Here, both are plotted verses f.

f is repeated.

Page 12: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

12

Basic Plotting (cont’d)

Notice that MATLAB chooses different line colors.

Page 13: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

13

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 x-

axis 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.

Page 14: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

14

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.

Page 15: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

15

Basic Plotting (cont’d)

If you specify plot(Y), where Y is a 2-

dimensional 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)’;

>>Y = [x .5*x.^2 (x.^3)/6];

Necessary so Y can be represented as 3 functions.

Page 16: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

16

Basic Plotting (cont’d)

>> plot(Y); grid Applies gridlines.

Note that a ‘;’ separates the two commands. A comma would also work.

Page 17: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

17

Basic Plotting (cont’d)

>> plot(x,Y), grid

Note change in x-axis.

Page 18: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

18

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.

Page 19: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

19

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 Point Types

Solid - Point .

Dashed -- Plus +

Dotted : Star *

Dashdot -. Circle o

x –mark x

Page 20: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

20

Line and Mark Styles (cont’d)

>>x = 1:10;

>>w = 2*x - 1;

>>plot(x,w,’x’)

Notice that the line is missing.

Page 21: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

21

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

Page 22: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

22

Line and Mark Styles (cont’d)

>>plot(x, Y, ‘+g’)

produces green ‘+’ symbols.

Colors

Yellow y

Magenta m

Cyan c

Red r

Green g

Blue b

White w

Black k

Invisible i

The order of the two symbols + and g is not important. +g is equivalent to g+.

Page 23: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

23

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)

>> xlabel(‘Real Part’)

>> ylabel(‘Imaginary Part’)

Recall: 1i assures that MATLAB views i as

regardless of a prior redefinition; e.g. i = 2.

1

Page 24: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

24

Complex Data Representation (cont’d)

Page 25: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

25

Polar PlotsThe 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))

Page 26: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

26

Polar Plots (cont’d)

Page 27: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

27

Plot Annotation and Legends (cont’d)

MATLAB allows you to mark a special data point,

such as a maximum value. So, for example,

>>[m,i] = max(z);

>>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)

Here, m returns the max value of z, while, i is its index.

Page 28: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

28

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)

Page 29: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

29

Figure Window Control (cont’d)

Page 30: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

30

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.

Page 31: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

31

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).

Page 32: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

32

Figure Window Control (cont’d)

Page 33: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

33

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.

Page 34: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

34

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 2-D 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 from the plot.

Page 35: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

35

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)

>>axis(A)

This can also be used with subplot functions.

Without the “;”, MATLAB would print out the axis limits - horizontal and vertical.

Page 36: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

36

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.

Page 37: 1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,

37

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.