Line Plots



The primary command for creating line plots has the form

plot(x, y, optionsString);

where x and y are vectors of data and optionString is a string indicating the plot options. If only one vector of data is passed, then the vector values will be plotted against their index. The options string consists of a concatation of the following characters.

y yellow . point - solid
m magenta o circle : dotted
c cyan x x-mark -. dashdot
r red + plus -- dashed
g green * star  
b blue s square  
w white d diamond  
k black v triangle (down)  
  ^ triangle (up)  
  < triangle (left)  
  p pentagram  
  h hexagram  

For example, to plot a solid red line with stars indicating data points one would used plot(x,y,'r*-').

To plot multiple lines, invoke plot with a triplet of arguments for each line plotted; e.g. use a plot command of the form

plot(x1,y1,opts1,x2,y2,opts2, ....)

Useful commands related to creating plots :

text(x,y,'String') Plots the string 'String' at (x,y). If x and y are vectors, then it plots the string at all pairs of coordinates. See text for details.
grid Adds grid lines to the current plot. See grid for details
axis Conrols axis formatting for the plot. See axis for details.
title, xlabel,
ylabel
Adds a title and labels to the plot. See title, xlabel, and ylabel for details.
hold Induces subsequent plots to be placed in the current plot frame. See hold for details.
subplot(...) Commands for creating multiple plots on a single page. See subplot for details,
figure, gcf,
clf
Commands for creating multiple figures (windows) for plots to be drawn into. See figure, gcf, and clf for details.

See Also : Printing/Saving plots

Samples

% Generating x,y data for a sin function, then plotting with a solid line

  x = 0.0:.1:2.0*pi;
  y = sin(x);
  plot(x,y,'-');

% Generating x,y data for sin and cos functions, then plotting in the same plot

  x = 0.0:.1:2.0*pi; 
  y = sin(x);
  z = cos(x);
  plot(x,y,'-',x,z,'g-') % plot cos with a green line

% Generating x,y data for sin and cos functions, then plotting in the same plot using
% the hold command

  x = 0.0:.1:2.0*pi; 
  y = sin(x);
  z = cos(x);
  plot(x,y,'-');
  hold on         % retain current plot for future plots
  plot(x,z,'g-')  % plot cos
  hold off        % release hold 

% Generating x,y data for a sin function and then plotting in a window
% with x and y ranges [0.0,2.0*pi] x [-1.5,1.5].

  x = 0.0:.1:2.0*pi;
  y = sin(x);
  plot(x,y,'-')
  axis([0.0,2.0*pi,-1.5,1.5]) % axis command applies to the current plot

% Creating a two separate plots on a single page.

  x = 0.0:.1:2.0*pi;
  y = sin(x);
  z = cos(x);
  subplot(2,1,1);
  plot(x,y,'-');
  subplot(2,1,2);
  plot(x,z,'-')

% (Use clf to get out of subplot mode)

% Creating plots on two separate pages. Windows can be managed from the GUI

  x = 0.0:.1:2.0*pi;
  y = sin(x);
  z = cos(x);
  plot(x,y,'-');  % plotting sin
  figure          % popping up a new window
  plot(x,z,'-');  % plotting cos


UCLA Mathematics Department ©2000