MATLAB Tutorial

Chapter 3. Basic graphing routines

3.1. 2-D plots

% The basic command for making a 2-D plot is "plot".
% The following code makes a plot of the function
% sin(x).
x = linspace(0,2*pi,200);
f1 = sin(x);
plot(x,f1)

% we now add a title and labels for the x and y axes
title('Plot of f_1 = sin(x)');
xlabel('x');
ylabel('f_1');

% Let us change the axes so that they only plot x from 0
% to 2*pi.
axis([0 2*pi -1.1 1.1]);  % [xmin xmax ymin ymax]

% Next, we make a new figure with cos(x)
f2 = cos(x);
figure;  % makes a  new figure window
plot(x,f2);
title('Plot of f_2 = cos(x)');
xlabel('x');
ylabel('f_2');
axis([0 2*pi -1.1 1.1]);

% Now, we make a single graph with both plots
figure;  % creates a new graph
plot(x,f1);
hold on;  % tells MATLAB not to overwrite current plot

% What happens if you forget to type hold on?
% "hold off" removes the hold.
plot(x,f2,'r');  % plots with red curve
title('Plots of f_1 = sin(x), f_2 = cos(x)');
xlabel('x');
ylabel('f_1, f_2');
axis([0 2*pi -1.1 1.1]);

% Now we add a legend.
legend('f_1', 'f_2');

% If we want to move the legend, we can go to the "Tools" menu
% of the figure window and turn on "enable plot editing" and
% then drag the legend to where we want it.

% Finally, we use the command "gtext" to add a line
% of text that we then position on the graph using our cursor.
gtext('f_1=f_2 at two places');

% The command "help plot" tells how to make a graph using various
% types of points instead of lines and how to select different
% colors.

clear all

3.2. 3-D plots

% First, we generate a grid containing the x and y values of
% each point.
x = 0:0.2:2*pi;  % create vector of points on x-axis
y = 0:0.2:2*pi;  % create vector of points on y-axis

% Now if n=length(x) and m=length(y), the grid will contain
% N=n*m grid points.  XX and YY are n by m matrices containing
% the x and y values for each grid point respectively.
[XX,YY] = meshgrid(x,y);

% The convention in numbering the points is apparent from
% the following lines.
x2 = 1:5; y2 = 11:15;
[XX2,YY2] = meshgrid(x2,y2);
XX2, YY2

% This shows that XX2(i,j) contains the jth component of the
% x vector and YY2(i,j) contains the ith component of the y
% vector.

% Now, we generate a function to save as a separate z-axis value
% for each (x,y) 2-D grid point.
Z1 = sin(XX).*sin(YY);  % calculate value of function to be plotted

% create a colored mesh plot
figure; mesh(XX,YY,Z1);
xlabel('x'); ylabel('y'); zlabel('z'); title('sin(x)*sin(y)');

% create a colored surface plot
figure; surf(XX,YY,Z1);
xlabel('x'); ylabel('y'); zlabel('z'); title('sin(x)*sin(y)');

% create a contour plot
figure; contour(XX,YY,Z1);
xlabel('x'); ylabel('y'); zlabel('z'); title('sin(x)*sin(y)');

% create a filled contour plot with bar to show function values
figure; contourf(XX,YY,Z1); colorbar;
xlabel('x'); ylabel('y'); zlabel('z'); title('sin(x)*sin(y)');

% create a 3-D contour plot
figure; contour3(XX,YY,Z1);
xlabel('x'); ylabel('y'); zlabel('z'); title('sin(x)*sin(y)');

clear all

3.3. Making complex figures

% Using the subplot command, one can combine multiple plots
% into a single figure. We want to make a master figure that
% contains nrow # of rows of figures and ncol # of figures
% per row. subplot(nrow,ncolumn,i) makes a new figure
% window within the master plot, where i is a number
% denoting the position within the master plot
% according to the following order :
% 1   2   3   ...  ncol
% ncol+1  ncol+2 ncol+3 ...  2*ncol

% First, generate the data to be plotted.
x = 0:0.2:2*pi;
y = 0:0.2:2*pi;
f1 = sin(x);
f2 = cos(y);
[XX,YY] = meshgrid(x,y);
Z1=sin(XX).*cos(YY);

% The following code creates a figure with four subplots.
figure;  % create a new figure

subplot(2,2,1);  % create 1st subplot window
plot(x,f1); title('sin(x)');
xlabel('x'); ylabel('sin(x)'); axis([0 2*pi -1.1 1.1]);

subplot(2,2,2);  % create 2nd subplot window
plot(y,f2); title('cos(y)');
xlabel('y'); ylabel('cos(y)'); axis([0 2*pi -1.1 1.1]);

subplot(2,2,3);  % create 3rd subplot window
surf(XX,YY,Z1); title('sin(x)*cos(y)');
xlabel('x'); ylabel('y'); zlabel('z');

subplot(2,2,4);  % create 4th subplot window
contourf(XX,YY,Z1); colorbar; title('sin(x)*cos(y)');
zlabel('x'); ylabel('y');

clear all