Introduction to Matlab & Data analysis

advertisement
Introduction to Matlab
& Data Analysis
Tutorials 4 & 5:
Simple Data Analysis and Graphics
Eilon Sharon, Weizmann 2008 ©
1
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
Code
Made in Matlab:
– Rocking Peaks example
2
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);
Min of coulmns.
C = min(A,B);
Element by element min
C = min(A,10);
Scalar expansion & min
C = min(A,[],dim);
Element along given dimension
[C,I] = min(...);
Retuens also index
The min function ignores NaNs!
Min([2, NaN]) == 2
3
Basic Data Analysis
A = [7, 8, 9;
4, 5, 6;
4, 5, 6];



What is the differences
between
[C,I]
[C,I]
C
C
the below?
= min(A)
= min(A(:));
= min(A,2);
= min(A,[],2);

C=
C=
C=
2
2
2
C=
7
4
4
4 5 6, I = 2 2 2
4, I = 2
2
2
2
2
2
2
4
Lecture Reminder
1


How to draw a plot
How to control:



0.8
0.6
0.4
0.2
axis
Annotations: title, xlabel,
ylabel, legend
Line specifications:
Symbol
b
g
r
c
m
y
k
Color
blue
green
red
cyan
magenta
yellow
black
0
-0.2
-0.4
-0.6
-0.8
-1
Symbol
.
o
x
+
*
s
d
v
^
<
>
p
h
0
2
Marker
point
circle
x-mark
plus
star
square
diamond
triangle (down)
triangle (up)
triangle (left)
triangle (right)
pentagram
hexagram
4
6
8
Symbol
:
-.
-(none)
10
12
14
16
Line style
solid
dotted
dashdot
dashed
no line
5
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
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
2
4
6
8
10
12
14
16
15
10
z

5
3
2
0
-3
1
0
-2
-1
-1
0
-2
1
2
3
-3
y
x
That is a lot! Lets break it down to pieces …
6
The Basic Components of a Figure
x = linspace(0,2*pi,101);
y = sin(x);
figure;
plot(x,y);





# - 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.
Plot of a data series
Plot
Axes
7
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; A filled plot
area(x,z)
hold on;
plot(x,y, 'r-');

Axes can hold several
graphs of different types

hold (switch on/off)

close(n) closes figure n


figure;

subplot(2,1,1);
plot(x,y,'b-');

subplot(2,1,2);
plot(x, y, 'b-', x, z, 'r:');
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?
8
Lets Make Our Sine Plot Good Looking


Controlling line properties
specifications
Annotating the graph





Basic plot:
figure;
plot(x,y);
Legend
Title
Axes labels
Adding text
Controlling the axes limits
Most of the functions are applicable
to other types of graphs!
9
Controlling Line Specifications

The third argument passes the



Line color
Marker type
Line style
plot(x, y, 'ro:');
Lets dive deeper
Symbol
b
g
r
c
m
y
k
Color
blue
green
red
cyan
magenta
yellow
black
Symbol
.
o
x
+
*
s
d
v
^
<
>
p
h
Marker
point
circle
x-mark
plus
star
square
diamond
triangle (down)
triangle (up)
triangle (left)
triangle (right)
pentagram
hexagram
Symbol
:
-.
-(none)
Line style
solid
dotted
dashdot
dashed
no line
10
Setting Line properties



Graphics elements are
represented by “objects”
plot(x,y, 'ro:', ...
'LineWidth',3,...
For example a data series line
'MarkerEdgeColor','k',...
is an “object”
'MarkerFaceColor',[.49 1 .63],...
We can change a property of
'MarkerSize',10);
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]
11
Annotating the Graph
plot(x,y, 'ro:', ...
'LineWidth',3,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',12);
Text properties
title('A Sine Plot', ...
'FontSize', 16, ...
'FontWeight', 'bold');
xlabel('Angle (radians)');
ylabel('Value');
legend('Sine');
Axes labels
Legend for multiple lines:
legend(‘line1’,’line2’);
12
Adding Text to Graphs
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');
X coordinate
Y coordinate
text(pi/6, sin(pi/6), …
['sin(\pi/6) = ' num2str(sin(pi/6))],…
'FontSize', 14);
A string.
“LaTex” format.
Search help for
“text properties” for
list of symbols
13
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 off
Grid on;

Can use also:




xlim([0,pi]);
ylim([-1,1]);
Box
Grid
14
Get/Set – Modifying the
Ticks and Ticks Labels
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 off
grid on;




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
x_labels = char('0 degrees','90 degrees','180 degrees');
set(gca, 'XTick', [0, pi/2, pi])
set(gca, 'XTickLabel', x_labels)
Property
Value
15
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
16
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)
Formats:
fig,
bmp,
jpg,
eps,
tif,
more…
saveas(figure_h, 'sine', 'jpg');% gcf
17
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
…
18
Lets Review Several
Types of 2D Graphs …
Eilon Sharon, Weizmann 2008 ©
19
Bar
“Kids actually believe that they can distinguish
between 21 different versions of pure sugar”
Jerry Seinfeld
C_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');
20
Hist /Histc
x=-3:0.1:3;
y = randn(1,1000);
figure;
subplot(3,1,1);
# bins
hist(y,50);
title('hist - k bins');
subplot(3,1,2);
Centers of bins
hist(y,x);
title('hist - bin centers given by x');
subplot(3,1,3);
edges of bins
[n,bin] = histc(y,x);
n=n/sum(n);
Converting to
bar(x,n);
title('histc & bar');
percent of values
Bin index for each element
21
Fill – Filling a Polygon

What does this code draws?
Fills a polygon
(connecting start to end)
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');
22
Errorbar & pie
figure; Like plot but with error bars
errorbar(x,y,e);
title('errorbar');
a = [ 10 , 5 , 20, 30]
figure;
subplot(2,1,1); Explode – logical array
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');
23
More plots



plotyy – two separate y scales
semilogx / semilogy – semilog plots
loglog - ….
24
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.
25
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
26
First Graph
27
Draw the First Graph - errorbar
Data analysis
avg_price_per_year = …
mean(gasoline_prices,2);
std_price_per_year = …
std(gasoline_prices,0,2);
Draw
figure;
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);
28
Second Graph
29
Draw the Second Graph – multiple plots
Data analysis
gas_price_90 = gasoline_prices(find(years == 1990),:);
gas_price_95 = gasoline_prices(find(years == 1995),:);
gas_price_00 = gasoline_prices(find(years == 2000),:);
Draw
plot(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;
30
Third Graph
31
Draw the Third Graph – hist / bar
Data analysis
[n xout] = hist(gasoline_prices(:), 20);
n = n/sum(n);
Draw
bar(xout,n);
ylabel('Fraction', 'FontSize', 14);
xlabel('$/Gallon', 'FontSize', 14);
title('Monthly price distribution', 'FontSize', 16);
axis tight;
32
Combine the Three Plots Using
Subplots
33
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
34
Color as a Third / Fourth Dimension





10
21
10
21
Matrices can be represented as
73
21
18
21
Images
10
4
8
21
Color map - a table of Nx3, where
3
21
10
45
each row represents a color in RGB
format
8
21
2
21
Scaling the values
colormap
to matrix indices
imagesc
colormap(‘gray');
colorbar
imagesc(X);
imagesc(X);colorbar;
colormap('default')
35
Examples of using imagesc
Jet flow
analysis
load flujet;
image(X);
colormap('jet');
Bone image
Microarray
illustration
load spine;
image(X);
colormap bone;
36
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
…
37
3D plot – plot3


Several graphs have a
3D equivalent.
Much of what we
learned for 2D graph
holds in 3D
z1
y1
x1
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
grid on
axis square
38
Lecture remainder - 3D surface graphs

Assume we want to illustrate
f ( x, y)  ( x  1)^2  y


xx = -3:0.1:3;
yy = -3:0.1:3;
[X, Y] = meshgrid(xx,yy);
In the range x=[-3,3],y=[-3,3] Z = (X+1).^2 - Y;
4 steps:
surf(X,Y,Z);
1.
Define the range of x (xx) and
Grid example:
the range of y (yy)
>> aa = [1, 2]
2.
Create grid matrices of x (X) and aa = 1
2
y (Y) using meshgrid.
>> bb = [3, 4, 5]
3.
Compute the function (Z) using X bb = 3
4
5
and Y.
4.
Use surf(X, Y, Z) / mesh(X, Y, Z)
>> [A, B]=meshgrid(aa,bb)
to draw the graph
A = 1
2 B = 3
3
Notice that the color is equivalent to
1
2
4
4
the Z dimension (“pseudo color”)
1
2
5
5 39
Running example




(  x2  y 2 )
Consider the function:
f ( x, y)  xe
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:





xx = -2:.2:2;
yy = -2:.2:3;
[X,Y] = meshgrid(xx,yy);
Z = X.*exp(-X.^2-Y.^2);
Play with colormap and add color bar


meshz(X,Y,Z);
meshc(X,Y,Z);
surfc(X,Y,Z);
contour(X,Y,Z);
contour3(X,Y,Z);
contourf(X,Y,Z);
40
Types of 3D Graphs
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');
Color as a
fourth
dimension
41
More About 3D graphs
figure; 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 off
title('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
42
Making Movies


Very easy!
Three commands:


M(1) = getframe
movie(M)
43
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;
Try it yourself!
(remember ctrl-C to stop the movie)
Play the movie ten times
movie(F,10);
% create an avi movie
movie2avi(repmat(F,1,5),…
'twin_peaks.avi', 'compression’,…
'Indeo3' );
44
3D Graphs - Summary




meshgrid
surf, mesh, contour
lighting , camlight, view
Getframe, movie, movie2avi
45
Download