# preserving the absolute location of an annotation after changing axis limits, or typing 'axis square'

2 次查看（过去 30 天）
Leo Simon 2020-4-30

I'm trying to draw an arrow using the recommended approach, i.e., using axxy2figxy, and maintain the flexibility to use commands like axis square and change the XLim properties after drawing my arrow. The code below illustrates two problems. First, after using the axis square command, the arrow is misaligned. Second, I can get my arrow in the right place, but if I then change the axis limits, the arrow stays in the same physical location, but I want it to move so that it maintains the same place relative to the changed axes. Such a simple requirement!!! The most important thing for me is to get a square axis rather than a rectangular one.
close all
xCoords = [-0.5,0];
yCoords = [-0.5,-0.5];
set(gca,'XLim',[-1,3]);
set(gca,'YLim',[-1,3]);
axis square
[axx,axy] = axxy2figxy(gca,xCoords,yCoords);
h = annotation('arrow',axx,axy);
disp('The arrow is in the wrong place because of the ''axis square'' command');
input('Hit any key to continue\n')
plot([-2,3],[-2,3],'color','w');
[axx,axy] = axxy2figxy(gca,xCoords,yCoords);
h = annotation('arrow',axx,axy);
disp('This kludge creates an arrow in the right place');
input('Hit any key to continue\n')
disp('Now resetting the xlimits shifts the axes but not the arrow');
set(gca,'XLim',[-3,3])

### 采纳的回答

Kelly Kearney 2020-5-1
You might want to try my line2arrow function; it allows you to an an arrowhead to a line (plotted with normal axis coordinates), and also sets up a buttondown function that updates the arrow location after making changes that misalign things (like changing the axis aspect ratio).
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
Leo Simon 2020-5-1
Very nice, thank you. I like the fact that you can reset the arrow by just repeating the command.

### 更多回答（1 个）

Ameer Hamza 2020-5-1
As far as i know, there is no direct way to do such a thing in MATLAB. The closed thing you would be able to do is to create a listener for the XLim and YLim. And change the position of arrows using axxy2figxy whenever the axes limit changes. This shows an example of how to create the listener: https://www.mathworks.com/matlabcentral/answers/92886-how-do-i-create-a-callback-function-which-gets-executed-whenever-the-xlim-value-of-axes-changes

### 类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

### Community Treasure Hunt

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

Start Hunting!

Translated by