-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_visualizing_histograms.m
60 lines (42 loc) · 1.25 KB
/
stats_visualizing_histograms.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
%%
% COURSE: Master statistics and machine learning: intuition, math, code
% URL: udemy.com/course/statsml_x/?couponCode=202112
%
% SECTION: Visualizing data
% VIDEO: Code: Histograms
%
% TEACHER: Mike X Cohen, sincxpress.com
%
%%
% a clear MATLAB workspace is a clear mental workspace
close all; clear
%% create data for the plot
% number of data points
n = 1000;
% generate log-normal distribution
data = exp( randn(n,1)/2 );
%% show a histogram
% number of histogram bins
k = 40;
figure(1), clf
% one way to show a histogram
histogram(data,k)
% another option
[y,x] = hist(data,k);
plot(x,y,'s-','linew',2,'markersize',12,'markerfacecolor','k')
xlabel('Value')
ylabel('Count')
%% movie showing histograms with increasing bins
bins2try = round( linspace(5,n/2,30) );
h = plot(x,y,'s-','linew',2,'markersize',12,'markerfacecolor','k');
xlabel('Value'), ylabel('Count')
for bini=1:length(bins2try)
% use hist to get histogram graph data for this bin count
[y,x] = histcounts(data,bins2try(bini));
% update xdata and ydata
set(h,'XData',x,'ydata',y);
% update title
title([ 'Histogram with ' num2str(bins2try(bini)) ' bins.' ])
pause(.5)
end
%% done.