subplot aligment automatic method
84 views (last 30 days)
Show older comments
giacomo labbri
on 5 Mar 2021
Commented: giacomo labbri
on 7 Mar 2021
Hi,
I would like to know if there is an easy way to align subplots ina figure. Here is my problem.
I have a figure with a subplot with a colorbar (on the right of the plot) and a subplot without a colorbar but with two y axis and relative labels bennith it. I would like the plot to be aligned (meaning the x values of the two subplot should be at the same horizontal location). The only way I found to achive that is to thinker with the 'Position' options of the subplot. Like:
subplot(4,2,6,'Position', [0.095 0.1 0.81 0.1428])
But thsi solution depends on the size of the figure itself and on the screen the figure is beign plotted to (at least that is my guess since I get different results dependig on the screen matlab is plotting it in and if I maximize the figure or not).
minimal working example of the problem:
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
figure
subplot(2,1,1)
imagesc(C)
colorbar
subplot(2,1,2)
hold on
yyaxis left
plot(C(:)); ylabel('C')
yyaxis right
plot(-C(:)); ylabel('-C')
Real case problem output
Any advice is appriciated!
4 Comments
Adam Danz
on 5 Mar 2021
Edited: Adam Danz
on 6 Mar 2021
> I would like to understand why this is not working
It does work in the sense of equating axis sizes. It's similar to method 1 in my answer. The problem is the linkaxes line which changes the xlim property which has nothing to do with axis position within the figure.
Accepted Answer
Adam Danz
on 5 Mar 2021
Edited: Adam Danz
on 5 Mar 2021
Method 1: equate axis sizes for all axes after adding colorbar
Size of axes without a colorbar will be set to the size of axes with a colorbar.
% Create Demo
ax = gobjects(2,3);
for i = 1:5
ax(i) = subplot(3,2,i);
imagesc(ax(i),rand(100,1000))
colorbar(ax(i))
end
ax(end) = subplot(3,2,6);
plot(ax(end), 1:1000,(1:1000).*rand(1,1000)*2)
% Set the width and height of all axes to the min width & height
allAxPos = vertcat(ax.Position);
allAxPos(:,3:4) = min(allAxPos(:,3:4)).*ones(numel(ax),1);
set(ax,{'position'},mat2cell(allAxPos,ones(numel(ax),1),4))
Method 2: Restore axis position and set colobar positions
The axes maintain their original sizes prior to adding colorbar. The colorbar position and width is adjusted.
function addOutsideColorbar(ax)
pos = ax.Position;
cb = colorbar(ax,'EastOutside');
ax.Position = pos;
cb.Position(1) = sum(ax.Position([1,3]))+.01;
cb.Position(3) = cb.Position(3).*.5;
end
Demo
ax = gobjects(2,3);
for i = 1:5
ax(i) = subplot(3,2,i);
imagesc(ax(i),rand(100,1000))
addOutsideColorbar(ax(i))
end
ax(end) = subplot(3,2,6);
plot(ax(end), 1:1000,(1:1000).*rand(1,1000)*2)
Method 3: Use tiledlayout
ax = gobjects(2,3);
tlo = tiledlayout(3,2);
for i = 1:5
ax(i) = nexttile(tlo);
imagesc(ax(i),rand(100,1000))
colorbar(ax(i))
end
ax(end) = nexttile(tlo);
plot(ax(end), 1:1000,(1:1000).*rand(1,1000)*2)
More Answers (0)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!