-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_visualizing_barplots.m
90 lines (66 loc) · 1.98 KB
/
stats_visualizing_barplots.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
%%
% COURSE: Master statistics and machine learning: intuition, math, code
% URL: udemy.com/course/statsml_x/?couponCode=202112
%
% SECTION: Visualizing data
% VIDEO: Bar plots
%
% TEACHER: Mike X Cohen, sincxpress.com
%
%%
% a clear MATLAB workspace is a clear mental workspace
close all; clear
%% create data for the bar plot
% data sizes
m = 30; % rows
n = 6; % columns
% generate data
data = bsxfun(@times,30*randn(m,n),linspace(-1,1,n).^2);
data = bsxfun(@plus,data,(1:n).^2);
%% show the bars
figure(1), clf
subplot(131)
bar(1:n,mean(data))
axis square, set(gca,'xlim',[0 n+1],'xtick',1:n)
title('Bar plot')
subplot(132)
errorbar(1:n,mean(data),std(data,[],1),'s')
axis square, set(gca,'xlim',[0 n+1],'xtick',1:n)
title('Errorbar plot')
subplot(133)
bar(1:n,mean(data))
hold on
errorbar(1:n,mean(data),std(data),'.')
axis square, set(gca,'xlim',[0 n+1],'xtick',1:n)
title('Bar+error plot')
%% manually specify x-axis coordinates
xcrossings = [ 1 2 4 5 6 9 ];
figure(2), clf
bar(xcrossings,mean(data))
hold on
errorbar(xcrossings,mean(data),std(data),'.')
axis square, set(gca,'xlim',[0 n+1],'xtick',1:n)
title('Bar+error plot')
%% note about bars from matrices
% data are groups (rows) X property (columns)
m = [2 5 4 3; 1 1 8 6];
figure(3), clf
% conceptualizing the data as <row> groups of <columns>
subplot(221)
imagesc(m), axis image
set(gca,'xtick',1:size(m,2),'ytick',1:size(m,1))
subplot(223)
bar(m)
set(gca,'xticklabel',{'Group 1';'Group 2'},'fontsize',10)
legend({'Property 1';'prop 2';'prop 3';'prop 4'})
title('Grouping by rows')
% now other orientation (property X group)
subplot(222)
imagesc(m'), axis image
set(gca,'xtick',1:size(m',2),'ytick',1:size(m',1))
subplot(224)
bar(m') % note the transpose
set(gca,'xticklabel',{'Property 1';'prop 2';'prop 3';'prop 4'},'fontsize',10)
legend({'Group 1';'Group 2'})
title('Grouping by columns (of original matrix)')
%% done.