필터 지우기
필터 지우기

subplot aligment automatic method

조회 수: 62 (최근 30일)
giacomo labbri
giacomo labbri 2021년 3월 5일
댓글: giacomo labbri 2021년 3월 7일
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
Adam Danz
Adam Danz 2021년 3월 5일
편집: Adam Danz 2021년 3월 6일
> 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.
giacomo labbri
giacomo labbri 2021년 3월 7일
Just for the record even commenting the linkaxes line I still get the same result. But your answer (see below) do work, so thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2021년 3월 5일
편집: Adam Danz 2021년 3월 5일
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)
  댓글 수: 1
giacomo labbri
giacomo labbri 2021년 3월 7일
thanks for the detailed aswers!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by