next up previous contents
Next: Handles Up: Graphics Previous: Graphics

2-D Graphics

The plot command suffices for basic 2-D graphics. If x and y are equal-length vectors, plot(x, y) will plot the values of y on the y-axis versus those of x on the x-axis. If the first argument is omitted, it will be taken to be the indices of y -- that is, 1:n where n is the length of y. Since graphs usually begin at zero, and MATLAB indexes from one, this is rarely wanted. If y has n elements, plot(0:n-1, y) may be more appropriate. A frequent usage is the form x = 0:.01:1, y = sin(2 * pi *x), plot(x, y). Parametrics, e.g. t = 0:.01:1, x = sin(t), y = cos(t), plot(x, y) work well, since the arguments just expand into the same vector.

To change the style of the lines, put in a third argument, in single quotes. E.g. plot(x, y, '-') specifies a solid line (the default). '-' is a dashed line, ':' dotted, '-.' alternating dots and dashes. To plot only the data points and not draw lines between them, put '.' (points), '+' (crosses), ' tex2html_wrap_inline2423 ' (stars), 'o' (circles), or 'x' (x's).

To specify a color, give the appropriate letter in single quotes as a third argument. To specify both color and line style, give both (in that order) as a single single-quoted argument, with no spaces; e.g. 'b:' is a blue dashed line. Available colors are white (w), black (k), red (r), green (g), blue (b), cyan (c), magenta (m), and yellow (y).

To put multiple pictures in the same graph window, simply give all their arguments to a single plot. E.g. plot(x1, y1, x2, y2) plots x1 versus y1 and x2 versus y2. plot(x1, y1, 'b:', x2, y2) does the same, but with the former as a dotted blue curve.

For multiple nonoverlapping plots in one window, use subplot(x, y, n), where x and y are each 1 or 2, to break the figure window into an x by y set of subgraphs, selecting the nth as the current subplot. MATLAB will rotate the current subplot clockwise by default (see gif Handles and use axes to override this; subplot 1 is in the upper left). Use subplot with no arguments to regain the single-plot figure window.

For multiple figure windows, figure n will make the nth figure window current, creating it if necessary; figure by itself creates the next figure window. Use gcf to see which figure is current.

Use clf and cla to clear the current figure or axes.

Replacing plot with loglog, semilogx, or semilogy produces a plot that is logarithmic in both axes, the x axis, or the y axis, respectively (non-logarithmic axes remaining linear). For other kinds of plots, use help on polar, bar, hist, quiver, compass, feather, rose, stairs, fill.

Both xlabel and ylabel take a text string (in single quotes) and use it to label the appropriate axis. Similarly title uses its text string argument as the graph's title. To place text arbitrarily, use gtext to place via mouse and text to place via specifying coordinates -- help for details. Use grid to get gridlines. Stylized versions are available as stext, sxlabel, stitle, etc (see help).

You can find the coordinates of a series of points with ginput by clicking with the mouse.

MATLAB will auto-scale the axes by default, using the minimum and maximum values of the data given to it for each axis. To examine or change this behavior, use axis. axis(xmin, xmax, ymin, ymax) sets the limits. v = axis returns in v the current limits, in the same order. axis(axis) prevents the graph from rescaling when you make a new plot, axis auto returns to auto-scaling, axis square uses the same scaling for both axes, axis equal uses the same scaling and ticmarks for both, and axis on and axis off turn scaling and ticmarks on and off. See help axis and help hold for details.

Given a function, fplot('function name', [xmin xmax]) will plot the function in the given range; this can offer a more convenient way to view a MATLAB-defined or M-file function than constructing vectors of values by hand. See help fplot. (Remember that MATLAB isn't big on symbolic things like this, though; a problem set asking you to plot a function probably wants it evaluated on a vector of points.)

Also see help plotxy.

next up previous contents
Next: Handles Up: Graphics Previous: Graphics

Sat Mar 21 21:42:28 EST 1998