Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: 3D plots Up: Graphics Previous: Graphics

General

Before you display graphics you have to calculate coordinates - unlike some other programs you can't just print sin(x) and give a range. Typically you'd do something like the following
  x=linspace(0, pi, 100);
  y=sin(x);
  plot(x,y);
The linspace command creates a vector of 100 elements equally spaced between 0 and pi inclusive.

Once you have a graph it's easy to add titles - use the menus on the graphics window or type commands like

   title('Trig');
   xlabel('Radians');
   ylabel('Sin');

You can plot more than one function on a graph. If you use hold on before doing another plot, the first plot won't be erased. Alternatively, you can give the plot command 2 lines at once. The advantage of this approach is that the lines are automatically drawn in different colours and that legends are easily created - e.g.

  x=linspace(0, pi, 100);
  y=sin(x);
  y2=cos(x);
  plot(x,y,x,y2);
  legend('Sin','Cos');
Adding a grid is easy - use grid on. The menu options let you change many features of graphs, but you can also use the axis command to change many axis features - range, aspect ratio, etc.

The plot command has many options to control colour, markers, etc. There's also scatter, bar, pie, stairs, stem, polar, compass, etc. Type help graph2d or (if you want to see some graphics!) see the Graphics tutorial

Note that you can have many figure windows open (read about the figure command). You can also have many graphs in each figure. The subplot command creates axes in tiled positions. So for example if you want 3 rows each containing 2 graphs, and you want to plot vector x in the bottom left graph (the 4th graph, counting left-to-right), you type

  subplot(3,2,4);
  plot(x);

Use clf to clear the current figure.


next up previous contents
Next: 3D plots Up: Graphics Previous: Graphics
© Cambridge University Engineering Dept
Information provided by Tim Love
2006-07-24