%% Graphics
%
%
%% The plot command
% In its simplest form plot(y) plots the data in vector y against their
% indices, e.g.
plot(rand(1,20))
%%
% Successive data points are joined by straight lines.
% 
% If y is a matrix the column are plotted agains the indices, e.g.
plot(rand(20,5))
%%
%
% The axes are automatically scaled so that all data points are included.
%
% The most common form is probably to plot x over y.
x = 0:pi/40:4*pi;
plot(x, sin(x))
%%
%
% To plot a straight line one can give the x and y coordinates at the end
% points, e.g., to draw a line between (0,1) and (4,3) use
plot([0 4],[1 3])

%% Labels
%
% One way of labeling is as follows:
gtext('example text')
%%
% A cross-hair appears on the screen and waits for a mouse button or
% keyboard key to be pressed.
%
% Text can also be added by specifying the coordinates using
% text(x,y,'text').
%
text(2, 2,'2nd example text')
%%
%
% If x and y are vectors, the text will be placed at each point.
%
% You can add grid lines, a title, and axis labels as follows
grid
title('example')
xlabel('x-axis')
ylabel('y-axis')
%%
% Different line styles, markers and colors can be specified, e.g.
plot(x, sin(x),'--.r')
%%
% See help plot for options.
%
%
%% Plotting multiple plots in the same axes
%
% Multiple plots can be added to the same axes by specifying the "hold on"
% command, or as follows
plot(x,sin(x),x,cos(x))

%%
%
% The following command allows you to have independent axis labels for the
% left and right y-axis.
plotyy(x,sin(x),x,2*cos(x))

%% Axis limits
%
% The scaling of axes can be specified explicitly with
% xlim([xmin xmax]),
% ylim([ymin ymax]), and
% axis([xmin xmax ymin ymax])
ylim([-1.5 1.5])
%%
% If you want to affect only one of the minimum or maximum use Inf or -Inf
% for the limits you don't want to specify.
%
% You can obtain the current vector of [xmin xmax ymin ymax]
ax = axis
yl = ylim
%%
%
% If you draw a circle with the following statements
clf
x = 0:pi/40:4*pi;
plot(sin(x),cos(x))
%%
% , it probably doesn't appear round. You can force the x- and y-axes to 
% the same length as follows.
axis equal
%%
% Undo by typing "axis normal"
%
% Turn axes on or off with "axis on" or
axis off

%% Plotting multiple plots in one figure window
%
% You can put multiple plots in one figure window with the command 
% subplot(m,n,p), e.g.
subplot(2,2,1)
plot(x,sin(x)), title('subplot(2,2,1)')
subplot(2,2,2)
plot(x,cos(x)), title('subplot(2,2,2)')
subplot(2,2,3)
plot(x,sin(2*x)), title('subplot(2,2,3)')
subplot(2,2,4)
plot(x,cos(x/2)), title('subplot(2,2,4)')

%% Graphical data input
%
% The command [x,y] = ginput allows you to read the (x,y) coordinates from 
% points on which you click on the graph. Terminate by pressing the 
% Enter key.
subplot(1,1,1)
plot(x,sin(x))
[x,y] = ginput
% add (x,y) points to plot
hold on
plot(x,y,'ro')

%% Logarithmic plots
%
% The commands semilogx and semilogy work like the plot command but will
% use a logarithmic (log10) scale for x- or y-axis, respectively. E.g.
%
close
x=0:.01:4;
semilogy(x,exp(x)), grid
%%
% For log-scales on both axes use loglog.
%
%% Plotting functions that change rapidly
%
% So far we have defined the increments in the independent variable x for
% plotting. If the function being plotted changes very rapidly in some
% places this can be inefficient or lead to wrong plots.
%
% For example, plot $y=sin(1/x)$.

x = 0.01:0.001:0.1;
xx = 0.01:0.0001:0.1;
plot(x,sin(1./x),xx,sin(1./xx))

%%
% Note how the graphs look different for x<0.04.
% 
% Instead we can use the function fplot, which evaluates the prescribed
% function more frequently where needed.

fplot('sin(1/x)', [0.01 0.1])


%% 3D plots
%
% The 3D version of plot is plot3(x,y,z), e.g.
plot3(rand(1,10),rand(1,10),rand(1,10))
%%
% Note the locations of the x-, y- and z-axes
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
%%
% comet3 is similar to plot3, but draws the image like a moving comet
comet3(rand(1,100),rand(1,100),rand(1,100))
%%
% The 2D version is called comet.
%
% Next, lets draw surfaces. Use meshgrid to create matrices of x and y
% coordinates, e.g.
[x,y]=meshgrid(0:0.5:5)
%%
% In this case the columns of matrix x will hold the x-coordinates of the
% data points in the grid, and the rows of y hold the y-coordinates.
%
% We can generate a 2D surface as follows
z = x.^2 - y.^2
%%
% Then plot with the command mesh
mesh(z)
%%
% In this case the data are plotted over the x and y indices. To use the
% proper values in x and y:
mesh(x,y,z)
%%
% An alternative to mesh is surf
surf(x,y,z)
%%
% The function view allows you to change the angle from which the object is
% viewed. In "view(az,el)" az is the azimuth or polar angle in the x-y
% plane in degrees. Changing az will rotate the viewing angle about the
% z-axis.
%
% The 2nd argument el refers to the elevation in degrees. el=90 means the
% viewpoint is directly overhead. (The default for 2D plots is view(0,90).)
view(30,30)
%%
% If you want isolines instead use
contour(x,y,z)
%%
% or 
contour3(x,y,z)
%%
% If you want to combine a mesh or surf plot with contours use meshc or
surfc(z,y,z)
%%
% For oceanographic data we often use pcolor
pcolor(x,y,z)
colorbar
caxis([-30 30])
%%
% The lines can be removed with 
shading flat
%%
%, and interpolated values can be plotted.
shading interp

%% Vector fields
% 
% You can draw vector fields with the function quiver.
%
% For example, consider the scalar function $V=x^2+y$. The gradient of $V$
% is given by 
%
% $\nabla V = (\frac{\partial V}{\partial x}, \frac{\partial V}{\partial y}) = (2x,1)$.
%
% You can draw arrows indicating the gradient of $V$ as follows:
clf
[x y] = meshgrid(-2:0.2:2, -2:0.2:2);
V = x.^2 + y;
dVdx = 2*x;
dVdy = ones(size(y));
contour(x,y,V)
hold on
quiver(x,y,dVdx,dVdy)
%% 
% See "help quiver" to find out how to scale the arrows etc.
%
% 
