How to create subplots with little vertical spacing?
177 ビュー (過去 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178022/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178023/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178024/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180401/image.png)
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!