Make same axes for subplots?

9 views (last 30 days)
Sahil Bajaj
Sahil Bajaj on 14 Jul 2018
Commented: dpb on 18 Jul 2018
Hi,
I have attached subplots with 1 row and 3 columns. For all subplots, I have identical X and Y axes. Is it possible to remove the Y axis of subplot 2 and 3 and just keep that for subplot 1 because it had same limit for all, so I just want to use common Y-axis for all subplots?
Thanks, Sahil

Accepted Answer

dpb
dpb on 14 Jul 2018
Edited: dpb on 17 Jul 2018
Presuming hF is the handle of the figure and hAx is the array of axes handles you saved when you created the subplots, you can do one of several effects depending on just what you want...
  1. set(hAx(2:3),{'YTickLabel'},{[]}) % remove only labels leaving ticks
  2. set(hAx(2:3),{'YTick'},{[]}) % remove ticks and labels from y axis
  3. set(hAx(2:3),{'YColor'},{hF.Color}); % remove the appearance of the y-axes entirely
You can also modify the 'Position' property to increase width of each and LH start point of the two right most to close up the spacing if desired.
Example adjusting limits
hAx=arrayfun(@(ix) subplot(1,3,ix),1:3); % make three subplots
hF=gcf; % get figure handle
set(hAx(2:3),{'YColor'},{hF.Color}) % hide axes for 2, 3
pos=get(hAx,'position'); % return the positions
Above is 3-element cell array for the three subplot axes.
In each position vector, the values are Left, Bottom, Width, Height so right side of axis position is pos(1)+pos(3)
>> pos{:}
ans =
0.1300 0.1100 0.2134 0.8150
ans =
0.4108 0.1100 0.2134 0.8150
ans =
0.6916 0.1100 0.2134 0.8150
rt=pos{3}(1)+pos{3}(3)-pos{1}(1); % rightmost axis RH end position
delt=pos{2}(1)-(pos{1}(1)+pos{1}(3)); % delta between 2 and 1; 3 and 2
delt=delt/2; % let's halve the present spacing
wnew=pos{1}(3)+delt; % so make the new width for all 3
pos{1}(3)=wnew;
pos{2}(3)=wnew;
pos{3}(3)=wnew;
pos{2}(1)=pos{1}(1)+pos{1}(3)+delt/2; % set LH position of second,
pos{3}(1)=pos{2}(1)+pos{2}(3)+delt/2; % third; split delta to match
set(hAx(1),'Position',pos{1}) % now set the three new positons
set(hAx(2),'Position',pos{2})
set(hAx(3),'Position',pos{3})
I'm certain one could sit down and derive the algebra to compute the LH positions more directly; I just did this by manipulating the corner and width to set the next one at end of previous + the difference.
For comparison, after the above machinations
>> cell2mat(pos)
ans =
0.1300 0.1100 0.2471 0.8150
0.3939 0.1100 0.2471 0.8150
0.6579 0.1100 0.2471 0.8150
>>
It's observed that what has changed is the first and third columns; the first being Left, the third Width. Width is constant but larger (wider) and Left is adjusted to account but diff(pos(:,1)) is still constant to evenly space each horizontally but leaving the first at same position to leave room for the visible tick labels.
Result is
>>
  3 Comments
Sahil Bajaj
Sahil Bajaj on 17 Jul 2018
Great, thanks ! This is really helpful.
dpb
dpb on 18 Jul 2018
You're welcome...it's another one of those things that I suggested some 20+ yr ago TMW should make a way to do such machinations far more easily than by having to do all the grunt work at this level.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!