How to create subplots with little vertical spacing?
조회 수: 179 (최근 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!