✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.

Statistical Toolbox in MATLAB®

How to plot statistical data from MATLAB's Statistical Toolbox. Examples of plots using Matlab's Statistical Toolbox


% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

mu = [0:0.1:2];
X = normpdf(1.5,mu,1)
fig = figure;
plot(X)

resp = fig2plotly(fig, 'strip',false)
plotly_url = resp.url;
Inspired from Stack Overflow
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

[f,x]=hist(randn(10000,1),50);%# create histogram from a normal distribution.
g=1/sqrt(2*pi)*exp(-0.5*x.^2);%# pdf of the normal distribution

fig = figure
bar(x,f/trapz(x,f));
hold on
plot(x,g,'r');
hold off

resp = fig2plotly(fig, 'strip',false)
plotly_url = resp.url;
Inspired from Matlab Docs
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

mu = [0:0.1:20];


X = normcdf(mu);
fig = figure;
plot(X)

resp = fig2plotly(fig, 'strip',false)
plotly_url = resp.url;
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

sigma = 5;    % just an example value
n = 3*sigma;  % cutoff point
x = -n:n;
G = 1 / (sigma * sqrt(2 * pi)) * exp(-x.^2 / (2*sigma^2)); % 1D Gaussian
G2 = G' *  G; % 2D
contour(G2);  % make contour plot

resp = fig2plotly;
Inspired from Stack Overflow
% Learn about API authentication here: https://plotly.com/matlab/getting-started
% Find your api_key here: https://plotly.com/settings/api

k = 3;
Sigma = {'diagonal','full'};
nSigma = numel(Sigma);
SharedCovariance = {true,false};
SCtext = {'true','false'};
nSC = numel(SharedCovariance);
d = 500;
x1 = linspace(min(X(:,1)) - 2,max(X(:,1)) + 2,d);
x2 = linspace(min(X(:,2)) - 2,max(X(:,2)) + 2,d);
[x1grid,x2grid] = meshgrid(x1,x2);
X0 = [x1grid(:) x2grid(:)];
threshold = sqrt(chi2inv(0.99,2));
options = statset('MaxIter',1000); % Increase number of EM iterations

fig = figure;
c = 1;
for i = 1:nSigma;
    for j = 1:nSC;
        gmfit = fitgmdist(X,k,'CovarianceType',Sigma{i},...
            'SharedCovariance',SharedCovariance{j},'Options',options);
        clusterX = cluster(gmfit,X);
        mahalDist = mahal(gmfit,X0);
        subplot(2,2,c);
        h1 = gscatter(X(:,1),X(:,2),clusterX);
        hold on;
            for m = 1:k;
                idx = mahalDist(:,m)<=threshold;
                Color = h1(m).Color*0.75 + -0.5*(h1(m).Color - 1);
                h2 a= plot(X0(idx,1),X0(idx,2),'.','Color',Color,'MarkerSize',1);
                uistack(h2,'bottom');
            end
        plot(gmfit.mu(:,1),gmfit.mu(:,2),'kx','LineWidth',2,'MarkerSize',10)
        title(sprintf('Sigma is %s, SharedCovariance = %s',...
            Sigma{i},SCtext{j}),'FontSize',8)
        hold off
        c = c + 1;
    end
end

resp = fig2plotly(fig)
plotly_url = resp.url;
Inspired from Matlab Docs