gca while using subplots and sgtitle

6 ビュー (過去 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 Center および File ExchangeVector Fields についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by