How to create subplots with little vertical spacing?
174 次查看(过去 30 天)
显示 更早的评论
Hello
I want to do a figure in Matlab consisting of a grid of images (subplots). I have used the subaxis toolbox :
subaxis(4,6,1, 'Spacing', 0.03, 'Padding', 0, 'Margin', 0);
The problem is that the vertical space between the subplots (images) is too big.
How can this be solved? The horizontal spacing is ok but I want the vertical spacing the same as the horizontal spacing. With the above toolbox this seems not working.
I'm also interested in other solutions than the mentioned toolbox.
采纳的回答
Kelly Kearney
2015-8-13
The subaxis command allows you to specify different values for vertical and horizontal spacing. Try playing around with different values to get what you want. Remember that the spacing/padding/margins are defined in terms of normalized coordinates, so if your figure isn't square, the vertical and horizontal distances won't be quite the same.
For example, the following will give you very little vertical spacing:
iax = 1; % Or whichever
subaxis(4, 6, iax, 'sh', 0.03, 'sv', 0.01, 'padding', 0, 'margin', 0);
7 个评论
Kelly Kearney
2015-8-17
I've added a bunch of comments to the example above to walk you through exactly what each line does.
更多回答(2 个)
Cedric
2015-8-13
编辑:Cedric
2015-8-13
You can store/use the axis handles of both subplots and access/modify their properties. Here is an example:
t = 0:0.1:10 ;
hAxis(1) = subplot( 2, 1, 1 ) ;
plot( t, sin(t) ) ;
hAxis(2) = subplot( 2, 1, 2 ) ;
plot( t, cos(t) ) ;
This produces:
Then you can access axes properties through their handles. E.g. for querying all available properties:
>> set( hAxis(1) )
ALim: {}
ALimMode: {'auto' 'manual'}
ActivePositionProperty: {'position' 'outerposition'}
...
Browsing all properties, you realize that the Position property is probably what you need to update. For getting a specific property, e.g. Position:
>> pos = get( hAxis(1), 'Position' )
pos =
0.1300 0.5838 0.7750 0.3412
where the first two coordinates are the horizontal and vertical relative (in [0,1]) coordinates of the origin of the axis with respect to the lower left corner of the figure, and the second two are the relative width and height. Say you want to make the first axis taller and closer to the second:
>> pos(2) = 0.5 ; % Shift down.
>> pos(4) = 0.45 ; % Increase height.
>> set( hAxis(1), 'Position', pos ) ;
and when you execute it, you get:
PS: I would favor this type of approache over 3rd party functions, because it is easy to learn using handles, and then you are really free to design almost whatever you want (e.g. small inserts in larger plots). For this purpose, the last thing that you need to know is the AXES function, which creates a new axes graphic object in a figure. To illustrate:
hAxis(3) = axes( 'Position', [0.66, 0.55, 0.2, 0.2] ) ;
[X,Y,Z] = peaks( 15 ) ;
surf( X, Y, Z);
adds the following insert:
2 个评论
Cedric
2015-8-14
编辑:Cedric
2015-8-14
Last example, for the fun of it:
load clown ;
nRows = 3 ;
nCols = 5 ;
% - Create figure, set position/size to almost full screen.
figure() ;
set( gcf, 'Units', 'normalized', 'Position', [0.1,0.1,0.8,0.8] ) ;
% - Create grid of axes.
[blx, bly] = meshgrid( 0.05:0.9/nCols:0.9, 0.05:0.9/nRows:0.9 ) ;
hAxes = arrayfun( @(x,y) axes( 'Position', [x, y, 0.9*0.9/nCols, 0.9*0.9/nRows] ), blx, bly, 'UniformOutput', false ) ;
% - "Plot data", update axes parameters.
for k = 1 : numel( hAxes )
axes( hAxes{k} ) ;
image( X ) ;
set( gca, 'Visible', 'off' ) ;
end
colormap( map ) ;
Here you see that it takes 2 lines of code for defining your own grid of axes. It is easy to wrap this into a function of your own, which fills the axes with whatever you need to plot/display.
William Chamberlain
2018-10-5
编辑:William Chamberlain
2018-10-5
jump to use subaxis if you just need to get it done right now (which is generally when I'm Googling for answers), but definitely come back and learn this ASAP, because this is a better solution than subaxis : you can easily set things up literally any way you want, and I'm kicking myself for not learning this a year ago.
Walter Roberson
2015-8-13
subaxis(4,6,1, 'Spacing', 0.03, 'Padding', 0, 'Margin', 0, 'SpacingVert', 0.03);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!