gca while using subplots and sgtitle

조회 수: 7 (최근 30일)
Manuel Wagner
Manuel Wagner 2021년 1월 9일
편집: Cris LaPierre 2021년 1월 10일
Hello,
i want to add arrows to the x and y axis of the subplots. This also works:
p = get(gca, 'Position');
anno = annotation('arrow');
anno.HeadStyle = 'plain';
set(anno, 'X', [p(1) p(1)], 'Y', [p(2) p(2)+p(4)]);
anno = annotation('arrow');
anno.HeadStyle = 'plain';
set(anno, 'X', [p(1) p(1)+p(3)], 'Y', [p(2) p(2)]);
If I give the subplots a title with "sgtitle", however, it no longer works and the arrow representation of the x-axis shifts.I would be very grateful for tips on solving the problem.
Many thanks, Manuel

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 1월 9일
편집: Cris LaPierre 2021년 1월 10일
It appears to actually be the combination of the plot title and the sgtitle. If you just have one or the other, it works fine. My recommendation would be to add the sgtitle first, then the subplots. This way, when you add the annotation, the axes are in their new position.
I did notice that I had to include a drawnow to force the update before getting the axes position.
figure()
sgtitle('Scenario 1: Test Title')
for c = 1:3
subplot(1,3,c)
scatter(randi(5,[1,5])-3,randi(9,[1,5])-5)
axis([-2 2 -4 4])
xlabel('x-axis')
ylabel('y-axis')
title("P_" + num2str(c))
drawnow
p = get(gca, 'Position');
anno = annotation('arrow');
anno.HeadStyle = 'plain';
set(anno, 'X', [p(1) p(1)], 'Y', [p(2) p(2)+p(4)]);
anno2 = annotation('arrow');
anno2.HeadStyle = 'plain';
set(anno2, 'X', [p(1) p(1)+p(3)], 'Y', [p(2) p(2)]);
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by