gca while using subplots and sgtitle
显示 更早的评论
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 个)
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
类别
在 帮助中心 和 File Exchange 中查找有关 Vector Fields 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
