subplot

Posted 2011.10.25 23:00

ex)
subplot(311);plot(.);

subplot(312);plot(.);

subplot(313);plot(.);

 


subplot -Create axes in tiled positions

Example of subplot function output

Alternatives

To add subplots to a figure, click one of the New Subplot icons in the Figure Palette, and slide right to select an arrangement of subplots. For details, see Plotting Tools — Interactive Plotting in the MATLAB Graphics documentation.

Syntax

h = subplot(m,n,p) or subplot(mnp)
subplot(m,n,p,'replace')
subplot(m,n,P)
subplot(h) 
subplot('Position',[left bottom width height])
subplot(..., prop1, value1, prop2, value2, ...)
h = subplot(...) 

Description

subplot divides the current figure into rectangular panes that are numbered rowwise. Each pane contains an axes object which you can manipulate using Axes Properties. Subsequent plots are output to the current pane.

h = subplot(m,n,p) or subplot(mnp) breaks the figure window into an m-by-n grid and creates an axes object in the pth location for the current plot, and returns the axes handle. The axes are counted along the top row of the figure window, then the second row, etc. For example,

subplot(2,1,1), plot(income)
subplot(2,1,2), plot(outgo)

plots income on the top half of the window and outgo on the bottom half. If the CurrentAxes is nested in a uipanel, the panel is used as the parent for the subplot instead of the current figure. The new axes object becomes the current axes.

subplot(m,n,p,'replace') If the specified axes object already exists, delete it and create a new axes.

subplot(m,n,P), where P is a vector, specifies an axes position that covers all the subplot positions listed in P, including those spanned by P. For example, subplot(2,3,[2 5]) creates one axes spanning positions 2 and 5 only (because there are no intervening locations in the grid), while subplot(2,3,[2 6]) creates one axes spanning positions 2, 3, 5, and 6.

subplot(h) makes the axes object with handle h current for subsequent plotting commands.

subplot('Position',[left bottom width height]) creates an axes at the position specified by a four-element vector. left,bottomwidth, and height are in normalized coordinates in the range from 0.0 to 1.0.

subplot(..., prop1, value1, prop2, value2, ...) sets the specified property-value pairs on the subplot axes object. Available property/value pairs are described more fully in Axes Properties. To add the subplot to a specific figure or uipanel, pass the handle as the value for the Parent property. You cannot specify both a Parent and a Position; that is,subplot('Position',[left bottom width height], 'Parent',h) is not a valid syntax.

h = subplot(...) returns the handle to the new axes object.

Tips

If a subplot specification causes a new axis to overlap a existing axis, the existing axis is deleted - unless the position of the new and existing axis are identical. For example, the statement subplot(1,2,1) deletes all existing axes overlapping the left side of the figure window and creates a new axis on that side—unless there is an axes there with a position that exactly matches the position of the new axes (and 'replace' was not specified), in which case all other overlapping axes will be deleted and the matching axes will become the current axes.

You can add subplots to GUIs as well as to figures. For information about creating subplots in a GUIDE-generated GUI, seeDisplaying Multiple Plots per Figure in the MATLAB Graphics documentation.

If a subplot specification causes a new axes object to overlap any existing axes, subplot deletes the existing axes object and uicontrol objects. However, if the subplot specification exactly matches the position of an existing axes object, the matching axes object is not deleted and it becomes the current axes.

subplot(1,1,1) or clf deletes all axes objects and returns to the default subplot(1,1,1) configuration.

You can omit the parentheses and specify subplot as

subplot mnp

where m refers to the row, n refers to the column, and p specifies the pane.

Be aware when creating subplots from scripts that the Position property of subplots is not finalized until either

  • drawnow command is issued.

  • MATLAB returns to await a user command.

That is, the value obtained for subplot i by the command

get(h(i),'position')

will not be correct until the script refreshes the plot or exits.

Special Case: subplot(111)

The command subplot(111) is not identical in behavior to subplot(1,1,1) and exists only for compatibility with previous releases. This syntax does not immediately create an axes object, but instead sets up the figure so that the next graphics command executes a clf reset (deleting all figure children) and creates a new axes object in the default position. This syntax does not return a handle, so it is an error to specify a return argument. (MATLAB implements this behavior by setting the figure'sNextPlot property to replace.)

Examples

Upper and Lower Subplots with Titles

To plot income in the top half of a figure and outgo in the bottom half,

income = [3.2,4.1,5.0,5.6];
outgo = [2.5,4.0,3.35,4.9];
subplot(2,1,1); plot(income)
title('Income')
subplot(2,1,2); plot(outgo)
title('Outgo')

Subplots in Quadrants

The following illustration shows four subplot regions and indicates the command used to create each.

figure
subplot(2,2,1)
text(.5,.5,{'subplot(2,2,1)';'or subplot 221'},...
    'FontSize',14,'HorizontalAlignment','center')
subplot(2,2,2)
text(.5,.5,{'subplot(2,2,2)';'or subplot 222'},...
    'FontSize',14,'HorizontalAlignment','center')
subplot(2,2,3)
text(.5,.5,{'subplot(2,2,3)';'or subplot 223'},...
    'FontSize',14,'HorizontalAlignment','center')
subplot(2,2,4)
text(.5,.5,{'subplot(2,2,4)';'or subplot 224'},...
    'FontSize',14,'HorizontalAlignment','center')

Asymmetrical Subplots

The following combinations produce asymmetrical arrangements of subplots.

figure
subplot(2,2,[1 3])
text(.5,.5,'subplot(2,2,[1 3])',...
    'FontSize',14,'HorizontalAlignment','center')
subplot(2,2,2)
text(.5,.5,'subplot(2,2,2)',...
    'FontSize',14,'HorizontalAlignment','center')
subplot(2,2,4)
text(.5,.5,'subplot(2,2,4)',...
    'FontSize',14,'HorizontalAlignment','center')

You can also use the colon operator to specify multiple locations if they are in sequence.

figure
subplot(2,2,1:2)
text(.5,.5,'subplot(2,2,1:2)',...
    'FontSize',14,'HorizontalAlignment','center')
subplot(2,2,3)
text(.5,.5,'subplot(2,2,3)',...
    'FontSize',14,'HorizontalAlignment','center')
subplot(2,2,4)
text(.5,.5,'subplot(2,2,4)',...
    'FontSize',14,'HorizontalAlignment','center')

Suppressing Axis Ticks

When you create many subplots in a figure, the axes tickmarks, which are shown by default, can either be obscured or can cause axes to collapse.

  • One way to get around this issue is to enlarge the figure to create enough space to properly display the tick labels.

  • Another approach is to eliminate the clutter by suppressing xticks and yticks for subplots as data are plotted into them as,

    h = subplot(10,1,1);
    set(h,'XTick',[],'YTick',[]);

    If the subplots are stacked, you can then display tick labels for the bottom axis.

Plotting Axes Over Subplots

Place a graph in the center, on top of four other graphs, using the axes and subplot functions. Data is random; your graphs can differ.

figure
y = zeros(4,15);
for k = 1:4
    y(k,:) = rand(1,15);
    subplot(2, 2, k)
    plot(y(k,:));
end
hax = axes('Position', [.35, .35, .3, .3]);
bar(hax,y,'EdgeColor','none')
set(hax,'XTick',[])

See Also

axes | cla | clf | figure | gca

How To

'Enginius > Matlab' 카테고리의 다른 글

hist (Histogram)  (0) 2011.10.26
fitting (polyfit, fit)  (0) 2011.10.25
subplot  (0) 2011.10.25
clear all / close all / clc / clf  (2) 2011.10.25
for loop  (0) 2011.10.25
normrnd (White Gaussian Noise sampling)  (0) 2011.10.25
« PREV : 1 : ··· : 57 : 58 : 59 : 60 : 61 : 62 : 63 : 64 : 65 : ··· : 68 : NEXT »