Reducing the distance among subplot figures?

I am using a subplot option to plot parts of a figure. Is there is a possibility to reduce the distance between the two figures, without affecting their dimensions (as shown by the arrows). For example, we can create more columns in subplot, but I am avoiding that. Thanks.

2 Comments

This is very very late for the answer but there is a very simple way to do this.
aa=subplot(122);
aa.Position(1)=0.51; %% position =[x_position y_position widht length] all are in some unit
you can check the position vectoor but simply typing aa in the console and itll display all the properties. from there you can start moving your figure. Please accept this answer because a lot many people are searching for this answer just like me but want a simple solution
Comments cannot be accepted. If you can write an answer with a small al visudemo of bringing the figures together, then that will be very helpful

Sign in to comment.

 Accepted Answer

DGM
DGM on 14 Aug 2021
This is a bit of a workaround, but I've been using it for a long time. It works in older versions and it doesn't require a bunch of tedious wrangling of plot properties.

1 Comment

Matt J
Matt J on 14 Aug 2021
Moved: Matt J on 5 Mar 2024
Here is yet another FEX offering that allows flexible control over subplot spacing:

Sign in to comment.

More Answers (2)

Set up axis position of subplots, e,g:
x=0:1:360; y=-180:1:180; G = cos(x)+sin(y(:));
HA(1) = subplot(221);
surf(G)
HA(2) = subplot(222);
mesh(G)
set(HA(1))
POS = get(HA(1), 'Position' )
POS(1) = 0.02 ; % Position move
POS(3) = 0.5 ; % Position move
set(HA(1), 'Position', POS) ;

4 Comments

FW
FW on 14 Aug 2021
Edited: FW on 14 Aug 2021
Thanks, it seems expand to the first figure by changing the width of the first one. I wanted that the dimensions of the figure remained the same and only their distance between them decreased. Is there a solution?
Maybe this is more like what you're after
x = rand(100,1);
% these two plots get adjusted
HA(1) = subplot(2,2,1);
plot(x)
HA(2) = subplot(2,2,2);
plot(x)
% these two plots are unchanged
subplot(2,2,3);
plot(x)
subplot(2,2,4);
plot(x)
gapscale = 0.5; % pick a number
P = vertcat(HA.Position);
gap = P(2,1)-(P(1,1)+P(1,3));
P(:,3) = P(1,3)+gap*(1-gapscale)/2;
P(2,1) = P(2,1)-gap*(1-gapscale)/2;
HA(1).Position = P(1,:);
HA(2).Position = P(2,:);
Note that the outer padding between the axes and the figure window is preserved. Only the horizontal space between the axes is being adjusted.
Thanks. It seems there is no simple solution. I basically wanted to introduce break x-axis by bringing two figures very close together without changing their dimensions (this was to be done in Powerpoint afterwards to hide the y-axis of the second one). It seems MATLAB is not a good choice for introducing axes breaks. I wanted to more subplots underneath.
I'm not really sure how close you wanted them or whether this could be done with a single axes. Should the plot boxes stay slightly separated? Did you want to maintain the box size or maintain the outer positions?
x = rand(100,1);
% bring these two together by stretching the boxes
HA(1) = subplot(2,2,1);
plot(x)
HA(2) = subplot(2,2,2);
plot(x)
gapscale = 0; % zero gap
P = vertcat(HA.Position);
gap = P(2,1)-(P(1,1)+P(1,3));
P(:,3) = P(1,3)+gap*(1-gapscale)/2;
P(2,1) = P(2,1)-gap*(1-gapscale)/2;
HA(1).Position = P(1,:);
HA(2).Position = P(2,:);
HA(2).YTick = [];
% bring these two together by moving the boxes
HA(1) = subplot(2,2,3);
plot(x)
HA(2) = subplot(2,2,4);
plot(x)
gapscale = 0; % zero gap
P = vertcat(HA.Position);
gap = P(2,1)-(P(1,1)+P(1,3));
P(1,1) = P(1,1)+gap*(1-gapscale)/2;
P(2,1) = P(2,1)-gap*(1-gapscale)/2;
HA(1).Position = P(1,:);
HA(2).Position = P(2,:);
HA(2).YTick = [];
Either way, you're going to have to deal with the fact that the ticklabels collide

Sign in to comment.

Not a direct answer to this question, but the newer tiledlayout method has greater flexibility in this regard. (For example, there is a TileSpacing property that can be set to "compact", which is what you want.)

2 Comments

Thanks. I think this is feature of 2021. I have 2019 and I am getting and error.
It was introduced in R2019b, so guessing you have R2019a.

Sign in to comment.

Products

Release

R2019a

Asked:

FW
on 13 Aug 2021

Moved:

on 5 Mar 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!