Eilon Sharon, Weizmann 2008 © 1 Introduction to Matlab & Data Analysis Tutorials 4 & 5:...

Preview:

Citation preview

Eilon Sharon, Weizmann 2008 © 1

Introduction to Matlab & Data Analysis

Tutorials 4 & 5: Simple Data Analysis and

Graphics

2

Goals

Learn to do a simple data analysis min, max, median, std

Learn how to visualize data Draw 2D graphs Control graphs properties

& annotate graphs Draw 3D graphs Create movies

Made in Matlab:

Code – Rocking Peaks example

3

Basic Data Analysis Useful data analysis functions:

min, max, mean, median, std, sum, prod, diff, sort Use doc to read about the functions lets focus on min examples

C = min(A);

C = min(A,B);

C = min(A,10);

C = min(A,[],dim);

[C,I] = min(...);

The min function ignores NaNs!

Min of coulmns.

Element by element min

Scalar expansion & min

Element along given dimension

Retuens also index

Min([2, NaN]) == 2

4

Basic Data AnalysisA = [7, 8, 9;

4, 5, 6;

4, 5, 6];

What is the differences

between the below? [C,I] = min(A) [C,I] = min(A(:)); C = min(A,2); C = min(A,[],2);

C = 4 5 6, I = 2 2 2 C = 4, I = 2 C = 2 2 2 2 2 2 2 2 2 C = 7 4 4

5

Lecture Reminder How to draw a plot How to control:

axis Annotations: title,

xlabel, ylabel, legend Line specifications:

0 2 4 6 8 10 12 14 16-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Symbol Color Symbol Marker Symbol Line styleb blue . point - solidg green o circle : dottedr red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star (none)

no liney yellow s squarek black d diamond v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram

6

Lecture Reminder Drawing more then one plot

on one axes system – hold on/off

Drawing more than one graph on a figure – subplot

Drawing different types of graphs – plot, pie, surf, bar, hist

Drawing 3D graphs – surf Using color in 3D graphs –

colormap Playing with the light –

headlight lighting Creating movies – getframe

, movie

0 2 4 6 8 10 12 14 16-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-3-2

-10

12

3 -3

-2

-1

0

1

2

3

0

5

10

15

y

x

z

That is a lot! Lets break it down to pieces …

7

The Basic Components of a Figure

# - A figure handle Menu & Toolbar

n = figure; creates a new figure figure(n); makes figure “n” the

current figure The current figure is the

target of the graphics output!

There are many options to edit the figure from the toolbar

Axes - a graphical object that contains graphs.

Axes Plot Plot of a data

series

x = linspace(0,2*pi,101);y = sin(x);figure;plot(x,y);

8

A Figure Can Contain Multiple Axes and an Axes Can Contain Multiple Graphs

x = linspace(0,2*pi,101);y = sin(x);z = cos(x);

figure;area(x,z)hold on;plot(x,y, 'r-');

figure;subplot(2,1,1);plot(x,y,'b-');subplot(2,1,2);plot(x, y, 'b-', x, z, 'r:');

Axes can hold several graphs of different types

hold (switch on/off) close(n) closes figure n Figure can contain

several axes (subplots). subplot – make the

subplot as current Index runs from left to

right row by row Another way to plot

several line series. Can you think of a third way?

A filled plot

9

Lets Make Our Sine Plot Good

Looking

Controlling line properties specifications

Annotating the graph Legend Title Axes labels

Adding text Controlling the axes

limitsMost of the functions are applicable to other types of graphs!

Basic plot:figure;plot(x,y);

10

Controlling Line Specifications

Symbol Color Symbol Marker Symbol Line styleb blue . point - solidg green o circle : dottedr red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star (none)

no liney yellow s squarek black d diamond v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram

plot(x, y, 'ro:');

The third argument passes the

Line color Marker type Line style

Lets dive deeper

11

Setting Line properties

plot(x,y, 'ro:', ... 'LineWidth',3,... 'MarkerEdgeColor','k',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',10);

Graphics elements are represented by “objects”

For example a data series line is an “object”

We can change a property of an object by passing to the function:

The property name as a string

The property value Colors can be represented

as RGB combination [R,G,B]

12

Annotating the Graphplot(x,y, 'ro:', ... 'LineWidth',3,... 'MarkerEdgeColor','k',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',12);

title('A Sine Plot', ... 'FontSize', 16, ... 'FontWeight', 'bold');xlabel('Angle (radians)');ylabel('Value');legend('Sine');

Text properties

Axes labels

Legend for multiple lines:

legend(‘line1’,’line2’);

13

Adding Text to Graphsplot(x,y, 'ro:', ... 'LineWidth',3,... 'MarkerEdgeColor','k',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',12);title('A Sine Plot', ... 'FontSize', 16, ... 'FontWeight', 'bold');xlabel('Angle (radians)');ylabel('Value');legend('Sine');

text(pi/6, sin(pi/6), …['sin(\pi/6) = ' num2str(sin(pi/6))],…'FontSize', 14);

X coordinate

Y coordinate

A string.

“LaTex” format.

Search help for

“text properties” for list of symbols

14

Controlling the Axes Limits

plot(x,y, 'ro:', ... 'LineWidth',3,... 'MarkerEdgeColor','k',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',12);title('A Sine Plot', ... 'FontSize', 16, ... 'FontWeight', 'bold');xlabel('Angle (radians)');ylabel('Value');legend('Sine');text(pi/6, sin(pi/6), ['sin(\pi/6) = …' num2str(sin(pi/6))],'FontSize', 14);

axis([0 pi -1 1 ])box offGrid on;

Can use also: xlim([0,pi]); ylim([-1,1]);

Box Grid

15

Get/Set – Modifying the Ticks and Ticks Labels

gca = get current Axes gcf = get current figure set/get – (temporary definition)

setting and getting the axes graphical object properties

Axes object has many properties

plot(x,y, 'ro:', ... 'LineWidth',3,... 'MarkerEdgeColor','k',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',12);title('A Sine Plot', ... 'FontSize', 16, ... 'FontWeight', 'bold');xlabel('Angle (radians)');ylabel('Value');legend('Sine');text(pi/6, sin(pi/6), ['sin(\pi/6) …= ' num2str(sin(pi/6))],'FontSize', 14);axis([0 pi -1 1 ])box offgrid on;

x_labels = char('0 degrees','90 degrees','180 degrees');set(gca, 'XTick', [0, pi/2, pi])set(gca, 'XTickLabel', x_labels)

Property

Value

16

Before and After …

Using the figure toolbar and plot tools Generating a .m file Why we should use the command line:

Programming is faster Documentation Reusability

17

Saving Figure .fig format; Save using:

Figure menu -> File -> save / save as , chose format

Use save or saveas function:

x = linspace(0,2*pi,41);y = sin(x);

figure_h = figure;plot(x,y)

saveas(figure_h, 'sine', 'jpg');% gcf

Formats:

fig,

bmp,

jpg,

eps,

tif,

more…

18

Matlab Has Many More Types of Graphs – Partial List

2D graphs: Plot plotyy Semilogx / semilogy Loglog Area Fill Pie bar/ barh Hist / histc Stem Errorbar Polar / rose Fplot / ezplot Scatter Image / imagesc /pcolor

3D graphs: Plot3 Pie3 Mesh / meshc / meshz Surf / waterfall / surfc Contour Quiver Fill3 Stem3 Slice Scatter3 …

Eilon Sharon, Weizmann 2008 © 19

Lets Review Several

Types of 2D Graphs

20

BarC_suger = [1 2 3];kid1_score = [7;8;9];kid2_score = [6;6;10];kid3_score = [5;5;10];kids_scores =[kid1_score,kid2_score, kid3_score];figure;subplot(3,1,1)bar(C_suger,kids_scores);title('bar ''group''');legend('Kid1','Kid2','Kid3');

subplot(3,1,2)bar(C_suger,kids_scores, 'stacked');title('bar ''stacked''');legend('Kid1','Kid2','Kid3');

subplot(3,1,3)barh(C_suger,kids_scores);title('group barh');legend('Kid1','Kid2','Kid3');

“Kids actually believe that they can distinguish between 21 different versions of pure sugar” Jerry Seinfeld

21

Hist /Histcx=-3:0.1:3;y = randn(1,1000); figure;subplot(3,1,1);

hist(y,50);title('hist - k bins');

subplot(3,1,2);

hist(y,x);title('hist - bin centers given by x');

subplot(3,1,3);

[n,bin] = histc(y,x);n=n/sum(n);bar(x,n);title('histc & bar');

# bins

Centers of bins

edges of bins

Converting to percent of values

Bin index for each element

22

Fill – Filling a Polygon What does this code draws?figure;t =(1:2:15)'*pi/8;x = sin(t);y = cos(t);fill(x,y,'r');axis square off;text(0,0,'STOP','Color', [1 1 1], ... 'FontSize', 80, ... 'FontWeight','bold', ... 'HorizontalAlignment', 'center');

Fills a polygon (connecting start to

end)

23

Errorbar & piefigure;errorbar(x,y,e);title('errorbar'); a = [ 10 , 5 , 20, 30]figure;subplot(2,1,1);pie(a, [0,0,0,1]);legend('A','B','C','D')

subplot(2,1,2);pie3(a, [0,0,0,1]);legend('A','B','C','D');

Like plot but with error bars

Explode – logical array

24

More plots plotyy – two separate y scales semilogx / semilogy – semilog plots loglog - ….

25

Example:

Dataset (load “gas.mat”):

years = 1976-2004 (29 years)

months = 12 months (char array)

gasoline_prices – gasoline prices in USA in

each month of the given years. (29*12)

Notice the NaN’s at 2004.

26

Exercise – Lets Draw the Following Figure

std

Title – font

size=16

Axis labels – font

size=14

Line width 2

20 bins

Fraction

Tight

Tip – manually

move the legend

27

First Graph

28

Draw the First Graph - errorbar

Data analysisavg_price_per_year = … mean(gasoline_prices,2);std_price_per_year = …

std(gasoline_prices,0,2);

Drawfigure;errorbar(years, avg_price_per_year, std_price_per_year, 'r-',

'LineWidth',2);xlabel('Year', 'FontSize', 14)ylabel('$/Gallon', 'FontSize', 14);title('Gas Price', 'FontSize', 16);

29

Second Graph

30

Draw the Second Graph – multiple plots

Data analysisgas_price_90 = gasoline_prices(find(years == 1990),:);gas_price_95 = gasoline_prices(find(years == 1995),:);gas_price_00 = gasoline_prices(find(years == 2000),:);Drawplot(1:12,gas_price_90, 'r-x','LineWidth',2);hold on;plot(1:12,gas_price_95, 'b:o','LineWidth',2);plot(1:12,gas_price_00, 'g--^','LineWidth',2);hold off;xlabel('Month', 'FontSize', 14);ylabel('$/Gallon', 'FontSize', 14);title('Monthly Prices at 1990, 1995, 2000', 'FontSize', 16);legend('1990','1995','2000');set(gca,'XTick',1:12, 'XTicklabel', months);grid; axis tight;

31

Third Graph

32

Draw the Third Graph – hist / bar

Data analysis[n xout] = hist(gasoline_prices(:), 20);n = n/sum(n);

Drawbar(xout,n);ylabel('Fraction', 'FontSize', 14);xlabel('$/Gallon', 'FontSize', 14);title('Monthly price distribution', 'FontSize', 16); axis tight;

33

Combine the Three Plots Using Subplots

34

2D Summary data analysis is very easy with matlab:

min, max, median, std Figure can contain several axes (subplots)

which can hold several data sets each Learn how to visualize data

Draw 2D graphs Control graphs properties & annotate graphs Draw 3D graphs Create movies

35

Color as a Third / Fourth Dimension

Matrices can be represented as Images

Color map - a table of Nx3, where each row represents a color in RGB format

colormap imagesc colorbar

10 21 10 21

73 21 18 21

10 4 8 21

3 21 10 45

8 21 2 21

imagesc(X);colorbar;colormap('default')

colormap(‘gray'); imagesc(X);

Scaling the values to matrix indices

36

Examples of using imagesc

Microarray illustration Bone image

Jet flow analysis

load spine; image(X);colormap bone;

load flujet;image(X);colormap('jet');

37

3D Graphs

Matlab has many types of 3D graphs

Most of them use color as a fourth dimension or to emphasis the z dimension

3D graphs: Plot3 Pie3 Mesh / meshc / meshz Surf / waterfall / surfc Contour Quiver Fill3 Stem3 Slice Scatter3 …

38

3D plot – plot3

Several graphs have a 3D equivalent.

Much of what we learned for 2D graph holds in 3D

t = 0:pi/50:10*pi;plot3(sin(t),cos(t),t)grid onaxis square

x1

z1

y1

39

Lecture remainder - 3D surface graphs

Assume we want to illustrate

In the range x=[-3,3],y=[-3,3]

4 steps:1. Define the range of x (xx) and

the range of y (yy)2. Create grid matrices of x (X)

and y (Y) using meshgrid.3. Compute the function (Z) using

X and Y.4. Use surf(X, Y, Z) / mesh(X, Y, Z)

to draw the graph

yxyxf 2)^1(),(xx = -3:0.1:3;yy = -3:0.1:3;

[X, Y] = meshgrid(xx,yy);Z = (X+1).^2 - Y;surf(X,Y,Z);Grid example:>> aa = [1, 2]aa = 1 2>> bb = [3, 4, 5]bb = 3 4 5

>> [A, B]=meshgrid(aa,bb)A = 1 2 B = 3 3 1 2 4 4 1 2 5 5

Notice that the color is equivalent to the Z dimension (“pseudo color”)

40

Running example Consider the function: Draw a mesh graph of the function in the

range: xx = -2:.2:2 , yy = -2:.2:3 What is the difference between mesh and surf? Try also:

meshz(X,Y,Z); meshc(X,Y,Z); surfc(X,Y,Z); contour(X,Y,Z); contour3(X,Y,Z); contourf(X,Y,Z);

Play with colormap and add color bar

)( 22

),( yxxeyxf

xx = -2:.2:2;yy = -2:.2:3;[X,Y] = meshgrid(xx,yy);

Z = X.*exp(-X.^2-Y.^2);

41

Types of 3D Graphs

Color as a fourth dimension

xx = -2:.2:2;yy = -2:.2:3;[X,Y] = meshgrid(xx,yy);Z = X.*exp(-X.^2-Y.^2);

Figure;… C = -X;surf(X,Y,Z,C);colorbar; title('surfc, C = -X');

42

More About 3D graphsfigure; subplot(2,2,1);Z_tag = Z;Z_tag(6:11,6:11) = NaN;surf(X,Y,Z_tag); title('surf with NaNs');

subplot(2,2,2);[dX,dY] = gradient(Z,.2,.2);contour(X,Y,Z), hold on, quiver(X,Y,dX,dY), hold offtitle('contour & quiver (of gradient)');

subplot(2,2,3);surf(X,Y,Z);title('surf, head camlight, phong lighting ');camlight headlight;lighting phong;

subplot(2,2,4);surf(X,Y,Z);title('surf, view Az=27, El=52');view(27,52);

The graphs can be also rotated with the figure toolbar

43

Making Movies

Very easy! Three commands:

M(1) = getframe movie(M)

44

Twin Peaks xx = -2:.2:2;yy = -2:.2:3;[X,Y] = meshgrid(xx,yy);Z = X.*exp(-X.^2-Y.^2); clear F;j = 1;surf(X,Y, sin(2*pi*j/20)*Z);zlim([-0.4218 0.4218]);F(j) = getframe;…j = 20;surf(X,Y, sin(2*pi*j/20)*Z);zlim([-0.4218 0.4218]);F(j) = getframe;

Play the movie ten timesmovie(F,10); % create an avi moviemovie2avi(repmat(F,1,5),…'twin_peaks.avi', 'compression’,… 'Indeo3' );

Try it yourself!

(remember ctrl-C to stop the movie)

45

3D Graphs - Summary

meshgrid surf, mesh, contour lighting , camlight, view Getframe, movie, movie2avi