|On this page…|
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 Figure Properties). See Figure Windows for more information.
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,
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.
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.
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:
Click on the subplot with the mouse.
Call subplot the m, n, i specifiers.
Call subplot with the handle (identifier) of the axes.
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
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:
colordef white — Sets the axis background color to white, the window background color to gray, the colormap to jet, surface edge colors to black, and defines appropriate values for the plotting color order and other properties.
colordef black — Sets the axis background color to black, the window background color to dark gray, the colormap to jet, surface edge colors to black, and defines appropriate values for the plotting color order and other properties.
colordef none — Set the colors to match that of MATLAB Version 4. This is basically a black background with white axis lines and no grid. MATLAB programs that are based on the Version 4 color scheme may need to call colordef with the none option to produce the expected results.
You can examine the colordef.m file to determine what properties it sets (enter type colordef at the MATLAB prompt).