Setting Up Figures

Creating Figure Windows

MATLAB® graphics are directed to a window that is separate from the Command Window. This window is referred to as a figure. The characteristics of this window are controlled by your computer's windowing system and MATLAB figure properties (see a description of each property). See Figure Properties for some examples illustrating how to use figure properties.

Graphics functions automatically create new MATLAB figure windows if none currently exist. If a figure already exists, that window is used. If multiple figures exist, one is designated as the current figure and is used (this is generally the last figure used or the last figure you clicked the mouse in).

The figure function creates figure windows. For example,

figure

creates a new window and makes it the current figure. You can make an existing figure current by clicking it with the mouse or by passing its handle (the number indicated in the window title bar), as an argument to figure.

figure(h)

Displaying Multiple Plots per Figure

You can display multiple plots in the same figure window and print them on the same piece of paper with the subplot function.

subplot(m,n,i) breaks the figure window into an m-by-n matrix of small subplots and selects the ithe subplot for the current plot. The plots are numbered along the top row of the figure window, then the second row, and so forth.

For example, the following statements plot data in four different subregions of the figure window.

t = 0:pi/20:2*pi;
[x,y] = meshgrid(t);
subplot(2,2,1)
plot(sin(t),cos(t)) 
axis equal
subplot(2,2,2)
z = sin(x)+cos(y);
plot(t,z)
axis([0 2*pi -2 2])
subplot(2,2,3)
z = sin(x).*cos(y);
plot(t,z)
axis([0 2*pi -1 1])
subplot(2,2,4)
z = (sin(x).^2)-(cos(y).^2);
plot(t,z)
axis([0 2*pi -1 1])

Each subregion contains its own axes with characteristics you can control independently of the other subregions. This example uses the axis function to set limits and change the shape of the subplots.

See the axes, axis, and subplot functions for more information.

Specifying the Target Axes

The current axes is the last one defined by subplot. If you want to access a previously defined subplot, for example to add a title, you must first make that axes current.

You can make an axes current in three ways:

For example,

subplot(2,2,2)
title('Top Right Plot')

adds a title to the plot in the upper right side of the figure.

You can obtain the handles of all the subplot axes with the statement

h = get(gcf,'Children');

The handles of all the axes are returned, with the most recently created one first. That is, h(1) is subplot 224, h(2) is subplot 223, h(3) is subplot 222, and h(4) is subplot 221. For example, to replace subplot 222 with a new plot, first make it the current axes with

subplot(h(3))

Default Color Scheme

The default figure color scheme produces good contrast and visibility for the various graphics functions. This scheme defines colors for the window background, the axis background, the axis lines and labels, the colors of the lines used for plotting and surface edges, and other properties that affect appearance.

The colordef function enables you to select from predefined color schemes and to modify colors individually. colordef predefines three color schemes:

You can examine the colordef.m M-file to determine what properties it sets (enter type colordef at the MATLAB prompt).

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS