gca while using subplots and sgtitle

8 views (last 30 days)
Manuel Wagner
Manuel Wagner on 9 Jan 2021
Edited: Cris LaPierre on 10 Jan 2021
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

Answers (1)

Cris LaPierre
Cris LaPierre on 9 Jan 2021
Edited: Cris LaPierre on 10 Jan 2021
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

Categories

Find more on Vector Fields 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!