%%Advanced Plotting

% Earlier in the class we saw how to plot basic line and scatter plots.
% MATLAB can also plot 3-d surfaces, bar and pie charts, etc.

% 3d line plot - plot3(x,y,z,LineSpec)
% Makes a 3d plot of a set of (x,y,z) data points
clear
close all
t = 0 : 0.1 : 6*pi;
x = cos(t);
y = sin(t);
z = t;
%'linewidth', 3 - property, value pair
plot3(x, y, z, 'b', 'linewidth', 3)
% puts axes in a box form around the plot
box on
xlabel('x(t)=cos(t)')
ylabel('y(t)=sin(t)')
zlabel('z(t)=t')

% Often want to visualize a surface of the form z = f(x,y)
% Need the equivalent of the many closely spaced x-values we used when
% plotting a function in 2-d

% Create a grid of points for plotting
clear
close all
x = 0 : 0.5 : 10;
y = -3 : 0.5 : 3;
%meshgrid creates a 2-d rectangular grid of X, Y points with X coordinates
%defined by x and Y coordinates defined by Y
[X, Y] = meshgrid(x, y);

% Visualizing the meshgrid using a scatter plot
% Reshape changes the dimension of the given matrix to the input size
% In this case X (Y in the second call) becomes a [length(x)*length(y),1]
% size vector
Xvec = reshape(X, length(x)*length(y), 1);
Yvec = reshape(Y, length(x)*length(y), 1);
% Need to reshape since scatter takes vectors as input
scatter(Xvec, Yvec)


% Contour plot - Visualizes 3-d info in 2-d
% Each contour has the same function value at each point on the contour

% 2-dimensional quadratic function
Z = X.^2 - 8*X + (1/2)*Y.^2 + (1/4)*X.*Y;
contour(X, Y, Z)

% Why is this reasonable?
% Consider a 1-d quadratic plot without a line connecting the points
xx = 0.5:0.05:1.5;
yy = xx.^2 - 2*xx;
plot(xx,yy, 'b.', 'markersize', 15)
% Can still see the function clearly
% Each pair of dots is equivalent to a contour (they have the same
% function value)




%3-d contour plot
%Same as contour but contours are drawn at cooresponding Z level
contour3(X, Y, Z, 10)


%Change Z to a trig function
Z = cos(X).*cos(Y);

%Mesh plot
% Creates a "wire frame", color helps illustrate the function value
mesh(X, Y, Z)

%Surface plot
surf(X, Y, Z)
%Equal data unit lengths along each axis
axis equal
%Display axis outline
box on
%Set transparency of surface, 0 <= val <= 1
alpha(0.5)
%3-D graph viewpoint specification (i.e., from what
%direction you are viewing it)
%view(3) to reset to default 3-D view, view([x, y, z]) is one option
view([3, 2, 1])



%Variations of mesh plots
clear
x = 0 : 0.1 : 5;
y = -2 : 0.1 : 2;
[X, Y] = meshgrid(x, y);
Z = 2*sin(X.^2) + cos(Y.^2 + X);

%subplot(rows, cols, loc) - creates a rows-by-cols matrix of plot areas
%and makes loc the current one
subplot(3, 1, 1)
mesh(X, Y, Z)

%Combination of mesh with contour
subplot(3, 1, 2)
meshc(X, Y, Z)

%Mesh with curtain
subplot(3, 1, 3)
meshz(X, Y, Z)

%Variations of surface plots
subplot(1, 2, 1)
surf(X, Y, Z)
axis equal

subplot(1, 2, 2)
%Surface with contour
surfc(X, Y, Z)
axis equal


%Bar plots
clear
close all

data = [2 4 3 6 1 3];
data3 = [[3 5 1 2 4]; [1 2 3 4 5]; [1 4 5 3 2]];

subplot(3, 1, 1)
bar(data)

subplot(3, 1, 2)
%horizontal bar plot
barh(data)

subplot(3, 1, 3)
%3d bar plot
bar3(data3);



%Histograms
close all
subplot(2, 2, 1)
bar(data)
title('Bar plot')

subplot(2, 2, 2)
hist(data)
title('Histogram (default)')

subplot(2, 2, 3)
%hist(X, N) – histogram for elements of X into N bins,
%or N = vec of bin center values
hist(data, 3)
title('Histogram (3 bins)')

subplot(2, 2, 4)
hist(data, 1:6)
title('Histogram (bins: 1,2,...,6)')



%Pie Charts
close all
subplot(1, 2, 1)
explode = [0 1 0 1 0 0];
pie(data, explode)

subplot(1, 2, 2)
%3d pie chart
pie3(data)

