clear
close all
clc

%% toy data first
x = (1:5) + random('norm', 0, 1, [1 5]);

figure; % h = figure
plot(x)
plot(x, '.') % overwrite previous plot
plot(x, '-g*','markersize', 12)
plot(x, '--mv', 'markersize', 12, 'linewidth', 2)
plot(x, '-.bo', 'markersize',14, 'linewidth', 3, ...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g')
xlabel('index')
xlabel('index', 'fontsize', 16)
ylabel('x', 'fontsize', 16)
ylim([0 6])
set(gca, 'fontsize', 16)
xticks(1:2:5);
xticks(1:5);
xticklabels({'a','b','c','d','e'});
xticklabels({'Los Angeles','San Francisco','San Jose','San Diego','Sacramento'})
%set(gca, 'xTick', 1:5, ...
%         'xTickLabels', {'Los Angeles','San Francisco','San Jose','San Diego','Sacramento'})
xtickangle(30)
ytickangle(90)
grid on
title('A MATLAB plot', 'fontsize', 18)
%axis equal

saveas(gcf, 'plot1', 'fig')

%% Import Iris data

type('iris.data')
type('iris.names')

fileID = fopen('iris.data');
C = textscan(fileID,'%f %f %f %f %s','Delimiter',',');
fclose(fileID);
whos C

X = [C{1:4}];
% X = cell2mat(C);

var_names = {'sepal length', 'sepal width', ...
    'petal length', 'petal width'};

class = C{5}; % C{5} is a cell array

save irisdata X class var_names

%% 1-D scatterplot for sepal length

sl = X(:,1);

figure; 
plot(sl, 'b.', 'markersize', 12)
xlabel('index')
ylabel('value (cm)')
set(gca, 'fontsize', 16)
title(['Iris data (' var_names{1} ')'],'fontsize', 18)

%% histogram for sepal length

figure
histogram(sl)
[cls_sizes, edges] = histcounts(sl);
histogram(sl, 'Orientation', 'horizontal')
histogram(sl, 10)
histogram(sl, 4:8)
histogram(sl, 'BinMethod', 'integers')
histogram(sl, 'Normalization', 'probability')

histogram(sl, 'Normalization', 'probability', ...
                  'EdgeColor', 'cyan', ...
                  'FaceColor', 'magenta')

% all the figure captions and font sizes can be 
% modified in the same way to enhance the plot
set(gca, 'fontsize', 16)
grid on
title('A histogram plot', 'fontsize', 20)
xlabel(var_names{1})
ylabel('frequency')

%% use subplots to stack two figures
figure; 

subplot(1,2,1) % 1 row, 2 columns, 1st plot
plot(sl, 'bo', 'markersize', 12, ...
               'MarkerEdgeColor','b',...
               'MarkerFaceColor','g')
xlabel('index')
ylabel('value (cm)')
set(gca, 'fontsize', 16)
title(['Iris data (' var_names{1} ')'],'fontsize', 18)

subplot(1,2,2) % 2nd plot
histogram(sl, 'Orientation', 'horizontal',...
                  'Normalization', 'probability', ...
                  'EdgeColor', 'cyan', ...
                  'FaceColor', 'magenta')

% all the figure captions and font sizes can be 
% modified in the same way to enhance the plot
set(gca, 'fontsize', 16)
grid on
title('A histogram plot', 'fontsize', 20)
xlabel(var_names{1})
ylabel('frequency')
%ylim([4 8])

%% Boxplot for sepal length

figure; boxplot(sl) 
%boxplot(sl,'orientation', 'horizontal')

set(gca, 'fontsize', 16)
title('A box plot', 'fontsize', 20)
xticklabels(var_names{1})

%% Plots for single categorical variable %%

%help categorical

class = categorical(class); % convert cell array to categorical array

tabulate(class)

levels = categories(class); % get levels of the categorical array
cls_sizes = countcats(class); % count number of elements at each level

%% make a histogram using the given categorical variable
figure
histogram(class, 'Normalization', 'count', ...
                 'EdgeColor', 'red', ...
                 'FaceColor', 'blue', ...
                 'BarWidth',0.5)
set(gca, 'fontsize', 16)
xlabel('class')
ylabel('count')
%xtickangle(45)
ylim([0 55])
grid on
title('Iris data', 'fontsize', 18)

% %make a histogram using only the counts
% figure
% histogram('Categories',levels,'BinCounts', cls_sizes)
% histogram('Categories',levels,'BinCounts', cls_sizes,...
%           'Normalization', 'probability', ...
%           'EdgeColor', 'red', ...
%           'FaceColor', 'blue',...
%           'BarWidth',0.5)
% set(gca, 'fontsize', 16)
% xlabel('class')
% ylabel('frequency')
% grid on
% title('Iris data', 'fontsize', 18)

%% bar plot (vertical)

figure
bar(cls_sizes)
bar(1:3, cls_sizes, 0.6, 'b')
xticklabels(levels)

bar(categorical(levels), cls_sizes)
bar(categorical(levels), cls_sizes, 0.6, 'b')

ylim([0 55])
set(gca, 'fontsize', 16)
xlabel('class', 'fontsize', 18)
ylabel('counts')
grid on
title('Bar plot for the sizes of the 3 Iris classes', 'fontsize', 20)

%% horizontal bar plot

figure
barh(cls_sizes)
xlim([0 55])
yticklabels(levels)
set(gca, 'fontsize', 16)
%ylabel('class', 'fontsize', 18)
ytickangle(60)
xlabel('counts')
grid on
title('Bar plot for the sizes of the 3 Iris classes', 'fontsize', 20)

%% Pie chart

figure
pie(class) % class is 1x150 categorical
pie(cls_sizes) % cls_sizes = [50 50 50]
pie(class, [1 0 0]) % explode only first slice
pie(class, levels) % explode all, same as [1 1 1]
pie(class, [1 1 1]) % all in explode mode 

h = pie(cls_sizes,[1 0 0]);
set(h(2:2:end),'FontSize',16);
set(h(1), 'FaceColor', 'red');
set(h(3), 'FaceColor', 'blue');
set(h(5), 'FaceColor', 'green');
legend(levels, 'fontsize', 16)

% 3-D pie chart
figure; % pie3(cls_sizes)
pie3(cls_sizes, [0 1 0]);
title('A 3-D Pie!', 'fontsize', 20)
legend(levels, 'fontsize', 16)
