subplot用法

subplot - Create axes in tiled positions

Example of subplot function output

GUI 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(...)
subplot(m,n,p,'v6')

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 matrix of small axes, selects the p th axes object 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 , bottom , width , 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.

Backward-Compatible Version

subplot(m,n,p,'v6') places the axes so that the plot boxes are aligned, but does not prevent the labels and ticks from overlapping. Saved subplots created with the v6 option are compatible with MATLAB 6.5 and earlier versions.

Use the subplot 'v6' option and save the figure with the 'v6' option when you want to be able to load a FIG-file containing subplots into MATLAB Version 6.5 or earlier.

  • Note    The v6 option enables users of Version 7.x of MATLAB to create FIG-files that previous versions can open. It is obsolete and will be removed in a future version of MATLAB.

See Plot Objects and Backward Compatibility for more information.

Remarks

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, see Creating Subplots in the MATLAB Creating Graphical User Interfaces 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

  • A 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's NextPlot 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.

Assymetrical Subplots

The following combinations produce asymmetrical arrangements of subplots.

subplot(2,2,[1 3])
subplot(2,2,2)
subplot(2,2,4)

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

subplot(2,2,1:2)
subplot(2,2,3)
subplot(2,2,4)

Suppressing Axis Ticks

When you create many subplots in a figure, the axes tickmarks, which are shown by default, can either be obliterated or can cause axes to collapse, as the following code demonstrates:

figure
for i=1:12
subplot(12,1,i)
plot (sin(1:100)*10^(i-1))
end

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. You can then label a single axes if the subplots are stacked, as follows:

figure
for i=1:12
subplot(12,1,i)
plot (sin(1:100)*10^(i-1))
set(gca,'xtick',[],'ytick',[])
end
% Reset the bottom subplot to have xticks
set(gca,'xtickMode', 'auto')

Plotting Axes Over Subplots

Place a plot in the center, on top of four other plots, using the axes and subplot functions:

for i = 1:4
subplot(2, 2, i)
plot(rand(1, 10));
end
axes('Position', [.35, .35, .3, .3]);
imshow('canoe.tif')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值