% Introduction to Plotting in MATLAB
% 09.29.2015 - Cameron Louttit
% ASEE at the University of Michigan
% Adapted from I. Mohedas, K. Ropella, and MathWorks

%% Before We Start...
% Let's start a diary!
diary('Intro to Plotting 20150929')

%% Graphics Handles
% Create a graphics handle F1 to explore the properties of handles
F1 = figure;
% What attributes does F1 have?
get(F1);

% What are each of these attributes? Reminder: we can use "help" or "doc"
help Alphamap
doc Alphamap

% Create a handle to the axes of our figure
F1axes = axes;

% Load EEG waveform data, a default dataset in MATLAB
load WAV_eeg_3_01_ORTH_CONT_1024
% Enable overlaid plots by renaming the variables
t1 = X; y1 = Y;
load WAV_eeg_3_01_ORTH_NONE_1024
t2 = X; y2 = Y;
clear X Y

% When we plot, if we don't suppress output, MATLAB will tell us some
% critical things about the plot
EEG1 = plot(t1,y1)
hold on % Allows us to overlay more plots
EEG2 = plot(t2,y2);

% To see more, let's use the "get" function:
get(EEG1)

% What if we want to play with some of these?
% Format: set(handle,'PropertyName','PropertyValue',...)
% Reminder: MATLAB sets many/all of these as optional variables and is OK
%           with you not specifying everything
set(EEG1,'LineWidth',5);
% We can combine specifications by continuing the pattern:
set(EEG1,'LineWidth',3,'LineStyle','-.');

% Multiple ways to play with color:
% Option 1: [R G B]
set(EEG1,'Color',[1 0 0]);
% Option 2: Store [R G B] as variable
chartreuse = [127/255, 255/255, 0/255]; % Note that these are normalized!
set(EEG1,'Color',chartreuse);
% Option 3: Use MATLAB's intelligence
set(EEG1,'Color','cyan');
% Option 3b: Abbreviations
set(EEG1,'Color','b');
% Where can I find out more about these abbreviations?
doc ColorSpec

% Let's combine all of this knowledge to adjust the two lines in our graph:
set(EEG1,'LineWidth',3,'Color','r','LineStyle','--');
set(EEG2,'LineWidth',3,'Color','b','LineStyle',':');

% Let's label the peak of our plot with a dot and text
[yMax,tMaxIndex] = max(y1);
tMax = t1(tMaxIndex);
MaxPoint = plot(tMax,yMax,'ko');
set(MaxPoint,'MarkerEdgeColor','k','MarkerFaceColor','k');
Fig1Text = text(tMax*1.2,yMax, 'Maximum');

% While altering the plotted line is great, we also want to inform our
% audience about the data that we're presenting. How about a title? 
title('EEG Waveforms');
% This works, but it's beneficial to have a handle attached - allows you
% the ability to manipulate the title down the line:
F1title = title('EEG Waveforms');

% Similarly, let's fill in the rest:
F1Xlabel = xlabel('Normalized Time');
F1Ylabel = ylabel('EEG Signal (microvolts)');
F1legend = legend(['Wave1'; 'Wave2']);

% These fonts are ugly. Let's change them.
set([F1title, F1Xlabel, F1Ylabel, F1legend, F1axes],...
    'FontName','Times New Roman');
set(F1title,'FontSize',16);

% These axes are weird too. Do you think we can change that?
% Remember, F1axis is a handle that might have some useful properties -
% let's check!
get(F1axes)
% 'XTick' and 'YTick' seem useful here:
set(F1axes,'XTick',[0:.25:1],'YTick',[-2:1:3]);

% Looks pretty good. Let's print the figure!
print(F1,'-dtiff','EEGFig')
% Notice that the figure is now a .tif file in your Current Folder.

% We can also copy to the clipboard
print(F1,'-clipboard','-dmeta')

%% Subplots
% Let's say that we want multiple plots together in a figure
clear
close all
clc

% Load a new dataset for this example
load flu.mat
data = [flu.NE, flu.Mtn, flu.Pac];
Regions = {'NE', 'Mtn', 'Pac'};

% Set up figure
F2 = figure;
set(F2,'units','normalized','Position',[0.1 0.1 0.8 0.8]);

% Make graphs in subplots
Ax(1) = subplot(4,3,1);
Flu(1) = plot(data(:,1));

Ax(2) = subplot(4,3,2);
Flu(2) = plot(data(:,2));

Ax(3) = subplot(4,3,3);
Flu(3) = plot(data(:,3));

Ax(4) = subplot(4,3,[4,5,6,7,8,9,10,11,12]);
Flu(3:5) = plot(data);

% The auto-generated limits are a little off...let's fix that using what we
% know!
set(Ax(:), 'XLim', [0 50],'XTick',[0 25 50],'XTickLabel',[0 25 50]);
set(Ax(4), 'YLim', [0 5], 'YTick',[0:5],'YTickLabel',[0:5]);

% Use a for loop to add axis labels
for i=1:4
    xLabMat(i) = xlabel(Ax(i),'Time (weeks)');
    yLabMat(i) = ylabel(Ax(i),'Flu (%)');
end

% And again for titles
for i = 1:3
    titleMat(i) = title(Ax(i), Regions{i});
end

% Overarching title for the whole plot
F2Title = suptitle('Flu Rates Across the US');

% Legend
Flu4Legend = legend(Ax(4),Regions);

% Print Figure
print(F2,'-dmeta', 'Flu')

%% Three-Dimensional Plots
% This is all great, but MATLAB can do much more, including 3D plotting. In
% order to use this, we need to first establish a meshgrid:
close all
clear
[X,Y] = meshgrid(-8:0.5:8);

% Let's use the classic demonstration of MATLAB's 3D plotting capabilities:
% a sinc function.
% NOTE: The following examples are done without declaring figures/axes just
% so that we are aware that we can code this way; however, if we wanted to
% manipulate anything about these figures, we would need those handles.
R = sqrt(X.^2+Y.^2)+eps;
Z = sin(R)./R;

% Create a mesh plot by using the 'mesh' command:
mesh(X,Y,Z);

% Let's make it look nicer with a surface plot:
surf(X,Y,Z)

% Maybe change the colormap a bit?
colorbar
colormap hot
colormap cool
colormap flag
colormap parula
% A reference for all included colormaps (you can always download more!)
doc colormap

% Finishing touches I: transparency! We use the 'alpha' command for this:
alpha(0)
alpha(1)
alpha(0.5)

% Finishing touches II: lighting/shading
close all
lightMat = {'left','right','headlight'};
F3 = figure;
set(F3,'units','normalized','position',[0.2,0.2,0.8,0.4]);
for i=1:1:3
   subplot(1,3,i)
   surf(X,Y,Z,'EdgeColor','none')
   title(lightMat{i});
   camlight(lightMat{i}); % Generates a light object from the direction specified
   lighting phong % An interpolation method to improve appearance
end

% Well this is all just a little too fancy for me. Maybe I need something a
% little simpler...
close all
contour(X,Y,Z)

% Notice this still abides by our colormap!
colormap hot

% We can specify a little bit more about our contour 
contour(X,Y,Z,15)

% What if our colormap isn't enough? Let's add values:
contour(X,Y,Z,'ShowText','on');

% Other fun stuff
contourf(X,Y,Z) % filled contour plot
contour3(X,Y,Z,20) % 3D contour plot
quiver(X,Y) % Quiver/Velocity plot
meshz(X,Y,Z) % draw a curtain around the mesh plot
waterfall(X,Y,Z) % unidirectional meshz
surfc(X,Y,Z) % surface overtop of contour plot

%% Finishing Up
% We must close out of the diary in order to view it later.
diary off