Upload
mostafa-ahmadi
View
214
Download
0
Embed Size (px)
Citation preview
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
1/94
Eran Eden, Weizmann 2008 © 1
Introduction to Matlab
& Data Analysis
Lecture 3:Simple Data Analysis and Graphics
Some of the slides are based on Introduction to Matlab by Dori Peleg
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
2/94
2
Today on the menu …
Previous lecture reminder
2D and 3D arrays continuation
Matlab Graphics - How to Visualize your data
2D graphics 3D graphics
Animation
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
3/94
3
Previous lecture reminder…
1D arrays
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
4/94
4
Indexing 1D Arrays Question: How do we retrieve/manipulate the content
of a specific element inside an array?
Answer: We use a mailbox like system called indexing
x(i) is the i’th element of x
1 2 3 4 5
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
5/94
5
Indexing 1D Arrays
x = 10 : -1 : 1;
y = x(3);disp(y);8
10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10
8
x y
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
6/94
6
Indexing 1D Arrays
We can retrieve more than one element at a time:
x = 10 : -1 : 1;
y = x([3,9,4]);disp(y)
10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10
8
x
y 2 7
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
7/947
Using indexing to edit 2D arrays
We can erase parts of an array
x = 10 : -1 : 1;
x(2 : 4) = [];disp(x);
10 9 8 7 6 5 4 3 2 1
10 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
8/948
Sub-array searching
Find the indexes of values in x that are larger than 3
>> x = [2 8 7 0 6 2 3];
>> find (x > 3)2 3 5
Find the actual values in x that are larger than 3
>> x = [2 8 7 0 6 2 3];
>> x(find (x > 3))
8 7 6
Very important tounderstand this example!
We can also get the same resultby breaking this into 2 steps:
>> inds = find(x > 3);
>> x(inds)
8 7 6
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
9/949
Previous lecture reminder…
Matrix
(2D Arrays)
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
10/9410
Indexing 2D arrays
x(h,w) is the h row and the w column
what is the value of x(3,4)?
10 21 10 4
73 21 18 10
10 4 8 21
3 21 10 45
8 21 2 21
3
4
what is the value of x(1 : 3, 4)?
10 21 10 4
73 21 18 10
10 4 8 21
3 21 10 458 21 2 21
3
4
21
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
11/9411
Indexing 2D arrays
>> a = [1 2 3; 4 5 6; 7 8 9];
>> disp(a)
1 2 3
4 5 6
7 8 9
>> a(1)
ans =
1
>> a(2)ans =
4
>> a(3)
ans =
7
It’s possible to index a 2D array using a single coordinate
>> a(5 : 8)
ans =
5 8 3 6
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
12/9412
Using indexing to edit 2D arrays
y =
0 0 0
0 0 0
0 0 0
The matrices on the left and right of the assignment (=) shouldmatch in size...
y = zeros(3,3);
x = ones(2,2);
Size 3 x 3 Size 2 x 2
x =
1 1
1 1
=
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
13/9413
Using indexing to edit 2D arrays
y =
0 0 0
0 0 0
0 0 0
The matrices on the left and right of the assignment (=) shouldmatch in size...
y = zeros(3,3);
x = ones(2,2);y(1 : 2, 1 : 2) = x
Size 2 x 2 Size 2 x 2
x =
1 1
1 1
=
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
14/9414
Using indexing to edit 2D arrays
y =
0 0 0
0 0 0
0 0 0
The matrices on the left and right of the assignment (=) shouldmatch in size...
y = zeros(3,3);
x = ones(2,2);y(1 : 2, 1 : 2) = x
1 1
1 1
x =
1 1
1 1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
15/9415
Using indexing to edit 2D arrays
y = zeros(3,3);
x = 4;y(1:2,1:2) = x
. . . unless the right matrix has a size of 1 x 1
y =
0 0 0
0 0 0
0 0 0
Size 2 x 2 Size 1 x 1
x =
4
=
4 4 4 4 4
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
16/9416
Using indexing to edit 2D arrays
y = zeros(5,5);
x = ones(2,2);
y = x
y =
1 1
1 1
... ORunless you are overwriting a matrix
Size 5 x 5 Size 2 x 2
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
17/9417
Using indexing to edit 2D arrays
y = zeros(3,3);
x = ones(3,3);y(1, 2 : 3) = x(1 : 2, 2 : 3)
??? Subscripted assignment dimension mismatch.
What happens when sizes of matrices don’t match?
y =
0 0 0
0 0 0
0 0 0
x =
1 1 1
1 1 1
1 1 1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
18/9418
Using indexing to edit 2D arrays
>> y = [1 2 3; 4 5 6; 7 8 9];
>> disp(y)
1 2 3
4 5 67 8 9
>> y(2, :) = [];
>> disp(y);
1 2 3
7 8 9
>> y(: , [1 3]) = [];
>> disp(y);
disp(y);
2
8
Erasing part of a matrix
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
19/9419
Simple operations on 2D arrays
Finding the size of a matrix
x =[ 2 4 6
3 6 9];
size(x)
2 3
Finding the length of a matrix
length(x)
3
Number of rows Number of columns
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
20/9420
Simple operations on 2D arrays
Finding the maximal numbers in each matrix column
>> x = [1 8 3; 7 2 6; 4 5 9]
>> max(x)
ans =
7 8 9
How do we get the maximal element in the entire matrix?
>> max(max(x))
ans =
9
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
21/9421
Finding things within arraysusing 1 coordinate indexing
>> x = [1 2 3;
7 8 9
4 5 6];
Find the indices of the values in x that are larger than 5.
>> inds = find (x > 5)
inds =
2 5 8 9
Find the actual values of all elements in x that are larger than 5.>> x(find(x > 5))
ans =
7 8 9 6
Alternatively we could break this intotwo steps:
>> inds = find(x > 5);
>> x(inds)
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
22/94
22
2D and 3D arrays continuation…
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
23/94
23
Arithmetic operations on 2D arrays The plus (+) and minus (-) operations are naturaly extended
to 2D arrays
Example 1:
x =[ 2 4 6
3 6 9];
y = x – 1
y =
1 3 5
2 5 8
Example 2:
x = x + x
x =
4 8 12
6 12 18
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
24/94
y =
1 3 42 4 7 24
Arithmetic operations on 2D arrays
Question: write a commmand that subtracts 1 from all the valuesin y that are larger than 4 and stores it back into y
y = [1 3 5
2 5 8];
Answer: y(find(y > 4)) = y(find(y > 4)) - 1
y(find(y > 4)) = y([4 5 6]) - 1
y(find(y > 4)) = [5 5 8] - 1
y(find(y > 4)) = [4 4 7]
y([4 5 6]) = [4 4 7]
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
25/94
(1) Scalar multiplication
price = [10 20 30; 2 3 4];
new_price = price * 2
new_price =
20 40 60
4 6 8
25
Arithmetic operations on 2D arrays
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
26/94
26
Arithmetic operations on 2D arrays
(2) Matrix multiplicaiton
price = [2 3 4; 2 4 5];
quantity = [4 1; 0 2; 2 1];
price * quantity
ans =
16 12
18 15
4 1
0 2
2 1
2 3 4
2 4 5
* = 16 12
18 15
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
27/94
27
Arithmetic operations on 2D arrays
(3) Element-by-element multiplicaiton
price = [2 3 4; 2 4 5];
quantity = [4 0 2; 1 2 1];
price .* quantity
ans =
8 0 8
2 8 5
2 3 4
2 4 5.* =
4 0 2
1 2 1
8 0 8
2 8 5
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
28/94
28
“Flattening” 2D arrays We can turn a matrix into a vector
(ordered according to columns)
x = [1 2 3
4 5 6
7 8 9];
y= [x(:, 1); x(:, 2); x(:, 3)]
y =
1
4
7
2
5
8
3
6
9
Alternatively the following command willdo the trick…
y = x(:)
y =
1
4
7
2
5
8
3
6
9
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
29/94
29
Final example In the matrix vac_days each column represents the number of vacations days an employee
took in the last 3 months.
In the matrix salaries each column represents that employee’s salary in the last 3 months.
Correct the salaries so that employees that took less then 3 days of vacation in a certainmonth get a 10% bonus to their salary.
>> vac_days = [1 5 10 0; 0 0 1 10 ; 0 0 5 7 ]
vac_days =
1 5 10 0
0 0 1 10
0 0 5 7
>> salaries = [6000 6000 7000 20000; 6000 6000 7000 20000; 7000 7000 7000 20000;]
salaries =
6000 6000 7000 20000
6000 6000 7000 200007000 7000 7000 20000
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
30/94
30
Final small example
Answer:
>> inds = find(vac_days < 3);
>> salaries(inds) = salaries(inds) * 110/100;
>> disp(salaries)
6600 6000 7000 22000
6600 6600 7700 20000
7700 7700 7000 20000
Or
>> salaries(find(vac_days
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
31/94
3D arrays…
31
10 21 10 21
73 21 18 21
10 4 8 213 21 10 45
8 21 2 21
10 21 10 21
73 21 18 21
10 4 8 21
3 21 10 45
8 21 2 21
10 21 10 21
73 21 18 21
10 4 8 21
3 21 10 45
8 21 2 21
Color images are 3D arrays
(We will learn this at the endof the course)
Why the heck do we need 3D arrays ?#@!?
R
G
B
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
32/94
32
3D arrays
All the (initialization / indexing / operations) that welearnt on 2D arrays can be applied to 3D arrays.
In fact, they can be applied to any N dimensionalarray.
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
33/94
33
3D arrays - example>> x = zeros(3,3,3)
x(:,:,1) =
0 0 0
0 0 0
0 0 0
x(:,:,2) =
0 0 0
0 0 0
0 0 0
x(:,:,3) =
0 0 0
0 0 0
0 0 0
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
34/94
34
3D arrays (example continued)>> x(:, :, 3) = ones(3,3)
x(:,:,1) =
0 0 0
0 0 0
0 0 0
x(:,:,2) =
0 0 0
0 0 0
0 0 0
x(:,:,3) =
1 1 1
1 1 1
1 1 1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
35/94
35
3D arrays (example continued)>> x(2, 2, :) = 3
x(:,:,1) =
0 0 0
0 3 0
0 0 0
x(:,:,2) =
0 0 0
0 3 0
0 0 0
x(:,:,3) =
1 1 1
1 3 1
1 1 1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
36/94
36
Tip of the day… improving your code readbility
Use spaces and tabs to make your code more clear
This code is ugly...
This code is pretty and readable!
age=10; weight=50;height= 1 :0.1:4;hair=2:0.1:5;
age = 10; weight = 50;height = 1 : 0.1 : 4;
hair = 2 : 0.1 : 5;
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
37/94
37
The Matlab graphics
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
38/94
Excel vs. Matlab Graphics
Why should we learn graphics and visualization inMatlab? Why not use Excel?
38
Matlabvisualization
Excelvisualization
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
39/94
39
Plot of dots plot is the most basic function for creating 2D graphics
plot(x1, y1, c1, x2, y2, c2, …)
x coordinate
of first dot
y coordinate of
first dot
Color & marker
of first dot
Symbol Color Symbol Marker Symbol Line styleb blue . point - solidg green o circle : dotted
r red x x-mark -. dashdotc cyan + plus -- dashedm magenta * star (none) no liney yellow s squarek black d diamond
v triangle (down)^ triangle (up)< triangle (left)> triangle (right)
p pentagramh hexagram
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
40/94
40
Plot of dots Example:
%Group #1
w_pre1 = [ 148 153 170 159 162]; %weight in previous month
w_cur1 = [ 90 85 92 91 88 ]; %weight in current month
%Group #2
w_pre2 = [157 172 179 167 179]; %weight in previous month
w_cur2 = [81 69 87 70 77 ]; %weight in current month
%Plotting the previous vs. current week weights of each contestantplot(w_pre1(1), w_cur1 (1),'bo', w_pre1(2), w_cur1 (2), 'bo', ...
w_pre1(3), w_cur1 (3), 'bo', w_pre1(4), w_cur1 (4), 'bo', ...
w_pre1(5), w_cur1 (5), 'bo', ...
w_pre2(1), w_cur2 (1),'r*', w_pre2(2), w_cur2 (2), 'r*', ...
w_pre2(3), w_cur2 (3), 'r*', w_pre2(4), w_cur2 (4), 'r*', ...
w_pre2(5), w_cur2 (5), 'r*');
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
41/94
41
Plot
145 150 155 160 165 170 175 18065
70
75
80
85
90
95
Notice that Matlab
automatically choosesthe axes borders thatfit the plot…
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
42/94
42
Plot Example:
%Group #1
w_pre1 = [ 148 153 170 159 162]; %weight in previous month
w_cur1 = [ 90 85 92 91 88 ]; %weight in current month
%Group #2
w_pre2 = [157 172 179 167 179]; %weight in previous month
w_cur2 = [81 69 87 70 77 ]; %weight in current month
plot(w_pre1(1), w_cur1 (1),'bo', w_pre1(2), w_cur1 (2), 'bo', ...w_pre1(3), w_cur1 (3), 'bo', w_pre1(4), w_cur1 (4), 'bo', ...
w_pre1(5), w_cur1 (5), 'bo', ...
w_pre2(1), w_cur2 (1),'r*', w_pre2(2), w_cur2 (2), 'r*', ...
w_pre2(3), w_cur2 (3), 'r*', w_pre2(4), w_cur2 (4), 'r*', ...
w_pre2(5), w_cur2 (5), 'r*');
This is very labor intensive… The same result can be achieved with much less work using vector notation.
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
43/94
43
Plot of dots using vectors
plot(x, y, c)
Example (continued)…
plot(w_pre1, w_cur1, 'bo');
hold on
plot(w_pre2, w_cur2, 'r+');
hold off
A vector containing
coordinates x1 … xn Color & markerof the line
A vector containing
coordinates y1 … yn
From now on all other plots will besuperimposed on the original figure
Cancel hold on. The following plot
will override current figureIf we omit the color and markerindication Matlab will try toconnect the dots
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
44/94
44
Plot of dots using vectors
145 150 155 160 165 170 175 18065
70
75
80
85
90
95
We get the exact same
plot
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
45/94
45
Plot (opening and closing)
Some Remarks:
Notice that every time we plot a figure it overrides theprevious figure (unless we use hold on)
If we want to open a new figure without erasing theprevious one we use a command called figure
If we want to close all the figures we use the commandclose all
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
46/94
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
47/94
47
Plot (adding labels)
145 150 155 160 165 170 175 18065
70
75
80
85
90
95
Previous month weight(kg)
C u
r r e n t m o n t h w e i g h t ( k g )
Weight contest , group 1 vs. group 2
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
48/94
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
49/94
49
Plot (adding labels)
60 80 100 120 140 160 18060
80
100
120
140
160
180
Previous month weight(kg)
C u r r e n t m o n t h w e i g h t ( k g )
Weight contest, group 1 vs. group 2
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
50/94
50
Plotting lines Example #1
x = 0 : 2 * pi;
y = sin(x);
plot(x, y);
0 1 2 3 4 5 6-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
By default Matlab willconnect the dots…
If we use: plot(x, y,'ro')
Matlab will display a dot plot
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
51/94
51
Plotting lines Example #2
x = 0 : 0.01 :2 * pi;
y = sin(x);
plot(x, y, 'r:')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
52/94
52
Plotting lines Example #3
x = 0 : 0.1 : 4*pi
y_sin1 = sin(x);
y_sin2 = sin(x + 0.2);y_sin3 = sin(2 * x);
plot(x, y_sin1);
0 2 4 6 8 10 12 14-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
53/94
53
Plotting lines Drawing several plots in one figure:
x = 0 : 0.1 : 4*pi
y_sin1 = sin(x);
y_sin2 = sin(x + 0.2);y_sin3 = sin(2 * x);
plot(x, y_sin1);
hold on
By default every new plot replacesthe previous plot
When hold on is used thesubsequent plots are made in thesame figure using the same axes
0 2 4 6 8 10 12 14-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
54/94
54
Plotting lines Drawing several plots in one figure:
x = 0 : 0.1 : 4*pi
y_sin1 = sin(x);
y_sin2 = sin(x + 0.2);y_sin3 = sin(2 * x);
plot(x, y_sin1);
hold on
plot(x, y_sin2, 'r');
0 2 4 6 8 10 12 14-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
55/94
55
Plotting lines
x = 0 : 0.1 : 4*pi
y_sin1 = sin(x);
y_sin2 = sin(x + 0.2);y_sin3 = sin(2 * x);
plot(x, y_sin1);
hold on
plot(x, y_sin2, 'r');
plot(x, y_sin3, 'm--');
0 2 4 6 8 10 12 14-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
56/94
56
Plotting lines
x = 0 : 0.1 : 4*pi
y_sin1 = sin(x);
y_sin2 = sin(x + 0.2);y_sin3 = sin(2 * x);
plot(x, y_sin1);
hold on
plot(x, y_sin2, 'r');
plot(x, y_sin3, 'm--');legend('sin(x)', 'sin(x + 0.2)', 'sin(2x)');
hold off
A figure legend can be added using the legend command
hold off restores default setting where each subsequent plot erases the previous one
0 2 4 6 8 10 12 14-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
sin(x)
sin(x + 0.2)
sin(2x)
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
57/94
57
You can make additional modificationsto your plot using the plot browser
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
58/94
58
You can make additional modificationsto your plot using the plot browser
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
59/94
Plotting multiple rows
The variable gene_vals contains the expression values of 170genes in 24 different samples.
59
gene_vals =
S1 S2 S3 S4 S5 S6 . . .
0.3767 0.4701 0.0175 0.0712- 0.0300 - 0.0220 . . .
0.5128 0.5367 0.0056 0.0179 0.0443 0.0291 . . .
0.4303 0.4447 0.0326 0.0498 0.1646 0.0490 . . . 0.4745 0.5575 0.1232 0.1444 0.0259 0.0187 . . .
0.2148 0.2380 0.1591 0.1438 0.1826 0.1717 . . .
0.4852 0.4029 0.0542 0.1435 0.1424 0.0546 . . .
0.4258 0.3948 0.0230 0.1261 0.0398 0.0199 . . .
. . .
Gene1
Gene2
Gene3
Gene4Gene5
Gene6
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
60/94
Plotting multiple rows
Plot the expression of the first gene>> gene_val = gene_vals(1, :)
>> plot(gene_val, '-*')
60 0 5 10 15 20 25-0.5
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
If the x coordinatesare NOT specified,
Matlab uses defaultcoordinates
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
61/94
Plotting multiple rows
Plot the expression of all the genes>> plot(gene_vals')
61
Plot can be used on a matrix.
It automatically generates a line foreach column (connecting all thepoints in the column).
Each line is assigned a different color.
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
62/94
62
Plotting other types of graphs
Matlab has many other types of plotting capabilities
x = -2 : 0.2 : 2;
y = x .* x;
bar(x, y, 'r');
title('Bar plot')
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.50
0.5
1
1.5
2
2.5
3
3.5
4Bar plot
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
63/94
63
Plotting other types of graphs
Plotting a histogram
norm_rand_values = randn(1, 1000);
hist(norm_rand_values, 20);
title('Histogram plot');
-3 -2 -1 0 1 2 30
20
40
60
80
100
120Histogram plot
Randomly generatednumbers from a normaldistribution with mean zeroand standard deviation one
randn(3) mean 0 std 1
ans =
-0.4326 0.2877 1.1892-1.6656 -1.148975 -0.03760.1253 1.1909 0.3273
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
64/94
64
Plotting other types of graphs
Plotting a pie chart
pie3([3000 2000 1000 5000],[0 0 0 1], ...
{'Bibi', 'Tsipi','Liberman','Donald Duck'});
title('Fraction of votes');
Votes vector
Donald Duck
Liberman
Fraction of votes
Barak
Bibi
Label vector-What are thosecurly braces?
“Explode” vector-which piece toseparate
Curly braces in this case define thedifferent titles as a cell array .
If we use:
title({'Fraction of', 'votes'});
We will get a two-line title.
Tsipi
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
65/94
65
Plotting other types of graphs
Making scatter plots
x1 = randn(1, 100);
y1 = randn(1, 100);
scatter(x1, y1, 25, [1 0 0], 'filled');
size ofeach point
Color ofeach point
Fill theinterior ofeach point
-3 -2 -1 0 1 2 3-4
-3
-2
-1
0
1
2
3
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
66/94
-3 -2 -1 0 1 2 3-4
-3
-2
-1
0
1
2
3
66
Plotting other types of graphs
-3 -2 -1 0 1 2 3-4
-3
-2
-1
0
1
2
3
Making scatter plots
x1 = randn(1, 100);
y1 = randn(1, 100);
scatter(x1, y1, 25, [1 0 0], 'filled');
hold onx2 = rand(1, 100) + 2;
y2 = randn(1, 100);
scatter(x2, y2, 25, [0 1 0] , 'filled');
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
67/94
-3 -2 -1 0 1 2 3-4
-3
-2
-1
0
1
2
3
67
Plotting other types of graphs
-3 -2 -1 0 1 2 3 4-6
-4
-2
0
2
4
6Scatter plot
Making scatter plots
x1 = randn(1, 100);
y1 = randn(1, 100);
scatter(x1, y1, 25, [1 0 0], 'filled');
hold onx2 = rand(1, 100) + 2;
y2 = randn(1, 100);
scatter(x2, y2, 25, [0 1 0] , 'filled');
x3 = rand(1, 100) + 3;
y3 = randn(1, 100) * 2;
scatter(x3, y3, 25, [0 0 1], 'filled');title('Scatter plot');
hold off
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
68/94
68
Plotting other types of graphs
There are many other types of plots and graphs…
Some will be taught in the tutorial Some will be taught in next lectures
All can be found in the Matlab guide and Google
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
69/94
69
Putting multiple plots in the same figure
subplot(# rows, # columns, current plot position)
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
70/94
70
Putting multiple plots in the same figure
figure;
subplot(2, 2, 1)x = -2 : 0.2 : 2;y = x .*x;
bar(x,y, 'r');
title('Bar plot')
subplot(2, 2, 2);norm_rand_values = randn(1, 1000);hist(norm_rand_values, 20);title('Histogram plot');
subplot(2, 2, 3);pie3([3 2 1 5],[0 0 0 1],{'Bibi','Barak','Liberman','Donald Duck'})title('Pie plot');
subplot(2, 2, 4);x1 = randn(1, 100)x2 = rand(1, 100) + 2
x3 = rand(1, 100) + 3
y1 = randn(1, 100);y2 = randn(1, 100);
y3 = randn(1, 100) * 2;
z = [repmat([1 0 0], 100, 1); ...repmat([0 1 0], 100, 1); repmat([0 0 1], 100, 1)];
scatter([x1 x2 x3], [y1 y2 y3], 10, z);
title('Scatter plot');
repmat =replicatematrix
z is a [300x3] matrixfor indicating color.
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
71/94
71
Putting multiple plots in the same figure
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
72/94
72
3D graphics – making surfaces
A 3D surface is defined as:
z = f(x, y)
We will create 3D surfaces using 2 functions: mesh(x, y, z);
surf(x, y, z);
x1
z1
y1
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
73/94
73
3D graphics – making surfaces
Exploring the behavior of a function using mesh: how does the following function
behave in the range -3 < x, y < 3?
2 22 ( )5 (sin( /15 )) 10 x y
x y e
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
74/94
xx =
-3 -2 -1 0 1 2 3
yy =
-3 -2 -1 0 1 2 3
x =
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
y =
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3z =
5.5225 5.5226 2.7279 1.0012 2.7279 5.5226 5.5225
5.5226 3.7647 1.8946 1.1832 1.8946 3.7647 5.5226
2.7279 1.8946 2.5695 4.6788 2.5695 1.8946 2.7279
1.0012 1.1832 4.6788 11.0000 4.6788 1.1832 1.0012
2.7279 1.8946 2.5695 4.6788 2.5695 1.8946 2.7279
5.5226 3.7647 1.8946 1.1832 1.8946 3.7647 5.5226
5.5225 5.5226 2.7279 1.0012 2.7279 5.5226 5.5225
74
3D graphics – making surfacesxx = -3 : 1 : 3
yy = -3 : 1 : 3
[x, y] = meshgrid(xx, yy)
z = 5 * sin(pi / 15 * x .* y).^2 + . . .
10 * exp( -(x.^2 + y.^2)) + 1
figure;
mesh(x, y, z);
xlabel('x'); ylabel('y'); zlabel('z');
-4-2 0
24
-5
0
50
5
10
15
xy
z
#rows
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
75/94
75
3D graphics – making surfaces
Lets use a finer grid…
xx = -3 : 0.2 : 3;
yy = -3 : 0.2 : 3;
[x, y] = meshgrid(xx, yy);
z = 5 * sin(pi / 15 * x .* y).^2 + 10 * exp( -(x.^2 + y.^2)) + 1;
figure;
mesh(x, y, z);
xlabel('x'); ylabel('y'); zlabel('z');
view(30, 50);
-3-2
-10
12
3 -3
-2
-1
0
1
2
3
0
5
10
15
y
x
z
view([az, el]) sets the angle of the view from
which an observer sees the current 3-D plot.az is the azimuth or horizontal rotation (degrees).el is the vertical elevation (degrees).
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
76/94
76
3D graphics – making surfaces
We can look at the actual surface
figure;
surf(x, y, z);
xlabel('x'); ylabel('y'); zlabel('z');
view(30, 50);
-3-2
-10
12
3 -3
-2
-1
0
1
2
3
0
5
10
15
y
x
z
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
77/94
77
3D graphics – making surfaces
Omitting the edges of the surface
figure;
surf(X, Y, Z, 'EdgeColor', 'none');
xlabel('x'); ylabel('y'); zlabel('z');view(30, 50);
-3-2
-10
12
3 -3
-2
-1
0
1
2
3
0
5
10
15
y
x
z
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
78/94
Headlight means at theangle of the camera.Phong is good for curvedsurfaces.
78
3D graphics – making surfaces
Playing with light…
figure;
surf(x, y, z, 'EdgeColor', 'none');
xlabel('x'); ylabel('y'); zlabel('z');
view(30, 50);camlight headlight
lighting phong
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
79/94
79
3D graphics – making surfaces
Lets make the grid even finer
xx = -3 : 0.01 : 3;
yy = -3 : 0.01 : 3;[x, y] = meshgrid(xx, yy);
z = 5 * sin(pi / 15 * x .* y).^2 + 10 * exp( -(x.^2 + y.^2)) + 1;
surf(x, y, z, 'EdgeColor', 'none');
xlabel('x'); ylabel('y'); zlabel('z');
view(30, 50);
camlight headlight
lighting phong
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
80/94
80
3D graphics – making surfaces
Playing with the colors…
Colors can be represented as a combination of R ed Green Blue.
R G B Color
1 0 0 Red
0 1 0 Green
0 0 1 Blue
0 0 0 Black1 1 1 White
1 1 0 Yellow
1 0.6 0.4 Copper
… … …
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
81/94
81
3D graphics – making surfacescolormap([1 0 0]) colormap([0 1 0])
colormap([0 0 1]) colormap([1 1 0])
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
82/94
82
3D graphics – making surfaces
show several surfaces on the same plotsurf(x, y, z, 'EdgeColor', 'none', 'FaceColor', 'red');
xlabel('x'); ylabel('y'); zlabel('z');
hold on;
z2 = x.^2 + y.^2 + 2;
surf(x, y, z2, 'EdgeColor', 'none','FaceColor', 'yellow', 'FaceAlpha', 0.7);
view(30, 40);
camlight headlight
lighting phong
1-opaque
0-transparent
U i 3D hi i li
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
83/94
83
Kaplan, Bren et al. Mol Cell. 2008
Mapping gene expression output function in response to a 2 inputsugar signal…
S u g
a r # 1
c o n
c e n t r a t i o n
Sugar #2concentration
Using 3D graphics to visualizeyour experimental data
U i 3D hi t i li
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
84/94
84
Using 3D graphics to visualizeyour experimental data
%Kaplan, Bren et al. Mol Cell. 2008
ara_bad = [
0.003 0.026 0.104 0.26 0.38 0.464 0.565 0.73 0.858 0.883 0.925 1
0.003 0.007 0.026 0.104 0.26 0.38 0.464 0.507 0.571 0.609 0.609 0.783
0.002 0.002 0.007 0.023 0.063 0.168 0.329 0.418 0.446 0.482 0.496 0.503
0.002 0.002 0.002 0.002 0.003 0.021 0.075 0.147 0.231 0.269 0.275 0.294
0.002 0.002 0.002 0.002 0.002 0.002 0.009 0.054 0.136 0.164 0.198 0.203
0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.004 0.037 0.079 0.124 0.137 0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.03 0.077 0.099
0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.004 0.03 0.077 ];
subplot(3, 1, 1);
surf(flipud(ara_bad));
xlabel('cAMP');ylabel('Arabinose');
camlight headlight
lighting phong
surf uses default xand y grid the size
of the matrix z.
Flip updown
U i 3D hi t i li
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
85/94
85
mal_t = [
0.928 0.982 0.979 0.945 0.912 0.903 0.89 0.888 0.934 0.95 0.962 0.961
1 0.932 0.876 0.825 0.805 0.78 0.785 0.788 0.797 0.821 0.821 0.896
0.789 0.813 0.783 0.775 0.753 0.743 0.734 0.727 0.721 0.733 0.726 0.787
0.591 0.583 0.548 0.541 0.521 0.501 0.485 0.498 0.489 0.501 0.539 0.554
0.483 0.44 0.419 0.408 0.387 0.388 0.399 0.405 0.40.434 0.444 0.507
0.325 0.296 0.278 0.266 0.245 0.235 0.234 0.243 0.255 0.274 0.30.276
0.127 0.168 0.164 0.171 0.166 0.152 0.164 0.174 0.193 0.216 0.249 0.267
0.198 0.179 0.164 0.146 0.128 0.119 0.11 0.106 0.109 0.13 0.146 0.185];
subplot(3, 1, 2);
surf(flipud(mal_t));
xlabel('cAMP');
ylabel('Maltose');camlight headlight
lighting phong
Using 3D graphics to visualizeyour experimental data
U i 3D hi t i li
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
86/94
86
gal_p = [
0.125 0.123 0.123 0.123 0.132 0.14 0.156 0.156 0.194 0.294 0.294 0.294
0.14 0.14 0.212 0.155 0.17 0.156 0.194 0.339 0.43 0.43 0.542 0.786
0.241 0.212 0.218 0.212 0.226 0.229 0.339 0.464 0.542 0.665 0.786 1
0.256 0.256 0.245 0.226 0.245 0.361 0.464 0.527 0.665 0.741 0.923 1
0.241 0.241 0.315 0.226 0.336 0.336 0.392 0.503 0.634 0.739 0.752 0.752
0.256 0.296 0.315 0.308 0.308 0.308 0.369 0.369 0.392 0.599 0.739 0.739
0.197 0.197 0.25 0.188 0.188 0.188 0.297 0.233 0.239 0.239 0.376 0.605 0.197 0.197 0.214 0.152 0.119 0.119 0.181 0.233 0.233 0.233 0.322 0.376
];
subplot(3, 1, 3);
surf(flipud(gal_p));
xlabel('cAMP');ylabel('Galactose');
camlight headlight
lighting phong
Using 3D graphics to visualizeyour experimental data
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
87/94
87
Animation
An animation is a sequence of images
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
88/94
88
Making simple animations
Example #1
x = 0 : 0.1 : 8 * pi
phi = 0.0; plot(x, sin(x + phi));
mov(1) = getframe;
phi = 0.2; plot(x, sin(x + phi));mov(2) = getframe;
phi = 0.4; plot(x, sin(x + phi));
mov(3) = getframe;
…
phi = 3.2; plot(x, sin(x + phi));
mov(17) = getframe;
movie(mov, 30, 10);
Number of timesthe movie is played
Frames per secondFrames
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
89/94
89
Making simple animations Example #1
x = 0 : 0.1 : 8*pi
phi = 0.0; plot(x, sin(x + phi));
mov(1) = getframe;
phi = 0.2; plot(x, sin(x + phi));mov(2) = getframe;
phi = 0.4; plot(x, sin(x + phi));
mov(3) = getframe;
…
phi = 3.2; plot(x, sin(x + phi));
mov(17) = getframe;
movie(mov, 30, 10);
movie2avi(mov, 'C:\sin_animation.avi');
Name of movie
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
90/94
90
Making simple animations
http://localhost/Eran/Eran%20teachings/Matlab%20and%20Data%20Analysis/scripts/Lecture3/files_for_animation/sin_animation.avi
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
91/94
91
Making simple animations Example #1
x = 0 : 0.1 : 8*pi
phi = 0.0; plot(x, sin(x + phi));
mov(1) = getframe;
phi = 0.2; plot(x, sin(x + phi));mov(2) = getframe;
phi = 0.4; plot(x, sin(x + phi));
mov(3) = getframe;
…
phi = 3.2; plot(x, sin(x + phi));
mov(17) = getframe;
movie(mov, 30, 10);
movie2avi(mov, 'C:\sin_animation.avi');
Remark: writing multiple copiesof similar lines is tedious andtime consuming!
In the next lecture we will see
how to circumvent this…
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
92/94
92
Animation Example #2:
% Animating climatology data
title('Sea Surface Temprature (SST)')
% Loading data
load sstjan.dat
15 18 20 21 23 20 21 23 20 21 23 21 19
16 14 21 22 32 21 31 32 21 31 32 23 20
15 15 22 30 32 22 30 32 22 30 32 28 24
12 13 24 29 30 24 29 30 24 29 30 29 …
15 18 20 21 23 20 21 23 20 21 23 21 19
16 14 21 22 32 21 31 32 21 31 32 23 20
15 15 22 30 32 22 30 32 22 30 32 28 24
12 13 24 29 30 24 29 30 24 29 30 29 …
15 18 20 21 23 20 21 23 20 21 23 21 19
16 14 21 22 32 21 31 32 21 31 32 23 20
15 15 22 30 32 22 30 32 22 30 32 28 24
12 13 24 29 30 24 29 30 24 29 30 29 …
x coordinate
y c o o r d i n a t e
Sea surfacetemperature atcoordinate (x,y)
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
93/94
93
Animation Example #2:
% Animating climatology data
title('Sea Surface Temprature (SST)')
% Loading data
load sstjan.dat
contourf(sstjan);
15 18 20 21 23 20 21 23 20 21 23 21 19
16 14 21 22 32 21 31 32 21 31 32 23 20
15 15 22 30 32 22 30 32 22 30 32 28 24
12 13 24 29 30 24 29 30 24 29 30 29 …
15 18 20 21 23 20 21 23 20 21 23 21 19
16 14 21 22 32 21 31 32 21 31 32 23 20
15 15 22 30 32 22 30 32 22 30 32 28 24
12 13 24 29 30 24 29 30 24 29 30 29 …
15 18 20 21 23 20 21 23 20 21 23 21 19
16 14 21 22 32 21 31 32 21 31 32 23 20
15 15 22 30 32 22 30 32 22 30 32 28 24
12 13 24 29 30 24 29 30 24 29 30 29 …
x coordinate
y c o o r d i n a t e
Sea surfacetemperature atcoordinate (x,y)
5 10 15 20 25 30 35
2
4
6
8
10
12
14
16
18
20
8/16/2019 Lecture 3 - Simple Data Analysis and Graphics
94/94
Animation Making a simple animation - example #2
% Animating climatology data
title('Sea Surface Temperature (SST)')
%cd 'C:\Eran\Eran teachings\Matlab and Data Analysis\scripts\Lecture3\files_for_animation' % Loading data
load sstjan.dat;
load sstfeb.dat;
load sstmar.dat;
load sstapr.dat;
load sstmay.dat;
load sstjun.dat;load sstjul.dat;
load sstaug.dat;
load sstsep.dat;
load sstoct.dat;
load sstnov.dat;
load sstdec.dat;
contourf(sstjan); frame(1) = getframe;
contourf(sstfeb); frame(2) = getframe;
contourf(sstmar); frame(3) = getframe;
contourf(sstapr); frame(4) = getframe;contourf(sstmay); frame(5) = getframe;
contourf(sstjun); frame(6) = getframe;
contourf(sstjul); frame(7) = getframe;
contourf(sstaug); frame(8) = getframe;
contourf(sstsep); frame(9) = getframe;
contourf(sstoct); frame(10) = getframe;
5 10 15 20 25 30 35
2
4
6
8
10
12
14
16
18
20
5 10 15 20 25 30 35
2
4
6
8
10
12
14
16
18
20
5 10 15 20 25 30 35
2
4
6
8
10
12
14
16
18
20
5 10 15 20 25 30 35
2
4
6
8
10
12
14
16
18
20
5 10 15 20 25 30 35
2
4
6
8
10
12
14
16
18
20
5 10 15 20 25 30 35
2
4
6
8
10
12
14
16
18
20