add arrowtext outside the figure
Show older comments
Hello
I want to add an arrow with a text outside of my figure plot, in the left side outeside of the y axis, The annotation command gives errors for using negative limits.
I need my annotation to be in the negative section of the figure: a vertical arrow with the text 'y' under the arrow:
annotation('textarrow',[-2 -2],[2.5 3],'String','y')
so my problem is how to define y and x values in order to be able to write left outside of the figure
Answers (1)
annotations are defined with respect to the figure, in the units of the figure.
plot(rand(1,10))
pos=get(gca,'Position')
outerpos=get(gca,'OuterPosition')
x = mean([outerpos(1) pos(1)])
y = [pos(2) pos(2)+pos(4)]
annotation('textarrow',[x x],y,'String','y')
4 Comments
mehra
on 13 Oct 2021
Basically the values you're going to give annotation are the proportion of the way across the figure...because the Units of the figure (by default) are set to normalized. So a x value of .5 means halfway across the figure. I specified the x position by taking the mean of the axes inner position (Position, which is sort-of the box around the axes) and the outer position (OuterPosition, which is the containing box that includes all the ticklabels etc.). You could specify whatever values you like. If you want it to be right on top of the y-axis, (i.e. the left of the axes box), then maybe ax=gca; ax.Position(1) would work well?
The reason you got very large values for gcf is because the units of gcf default to pixels. You can create your annotation in pixel units (or other units) by calling set and specifying 'Units', 'pixels' and 'X' and 'Y' values. There is, unfortunately, no option to specifyt an annotation's position in the axes's units. Also note that pixel units for an annotation are relative to the figure (0,0 is the lower left corner), and gcf's pixels are relative to the screen.
axes;
get(gca,'Units')
get(gcf,'Units')
annotation('Arrow',[.2 .2],[.1 .2])
h=annotation('Arrow');
set(h,'Units','pixels','X',[300 200],'Y',[150 100])
mehra
on 14 Oct 2021
You're correct about the first two values, but not exactly for the last two values. Position values in MATLAB are typically x,y,width,height.
Assuming your axes is directly in a figure, and not in another container (like a panel), and not created with the uiaxes function...
- The bottom left corner (of the 'inner' position) of the axes is 86.5% horizontally and 11% vertically from the bottom
- the width of your axes is about 4% of the width of the figure window
- the height of the axes is 81.5% of the height of the figure window
Here's a labeled diagram (with annotation) of all of those locations...and a few more that might be helpful depending on where exactly you're trying to place your arrow:
ax=gca;
plot(ax,randn(10,1))
xlabel('xlabel')
ylabel('ylabel')
title('title')
annotation('rectangle',[.001 0.001 .998 .998],'Color','m') % simulating figure boundary in online
ax.Position=[0.8651 0.1100 0.0399 0.8150];
annotation('rectangle',ax.Position,'Color','r','LineWidth',2)
annotation('doublearrow',[0 ax.Position(1)],[.5 .5],'Color','r')
annotation('doublearrow',[ax.Position(1)+ax.Position(3) 1],[.5 .5],'Color','r')
annotation('doublearrow',[0 ax.Position(1)],[.5 .5],'Color','r')
xm=ax.Position(1)+ax.Position(3)/2;
annotation('doublearrow',[xm xm],[0 ax.Position(2)],'Color','r')
annotation('doublearrow',[xm xm],[ax.Position(2)+ax.Position(4) 1],'Color','r')
annotation('rectangle',ax.OuterPosition,'Color','b','LineWidth',2)
annotation('doublearrow',[0 ax.OuterPosition(1)],[.6 .6],'Color','b')
annotation('doublearrow',[ax.OuterPosition(1)+ax.OuterPosition(3) 1],[.6 .6],'Color','b')
Categories
Find more on Printing and Saving 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!

