add arrowtext outside the figure

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)

Dave B
Dave B on 13 Oct 2021
Edited: Dave B on 13 Oct 2021
annotations are defined with respect to the figure, in the units of the figure.
plot(rand(1,10))
pos=get(gca,'Position')
pos = 1×4
0.1300 0.1100 0.7750 0.8150
outerpos=get(gca,'OuterPosition')
outerpos = 1×4
0 0 1 1
x = mean([outerpos(1) pos(1)])
x = 0.0650
y = [pos(2) pos(2)+pos(4)]
y = 1×2
0.1100 0.9250
annotation('textarrow',[x x],y,'String','y')

4 Comments

It is a bit complicated for me....
So I think i have to define x and y vlues before using annotation,
but I didnt get the reason for use of pos and outerpos, and how do you choose it to be y axis?
(I used gcf instead of gca (just to compare) and it gave me way large values! )
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')
ans = 'normalized'
get(gcf,'Units')
ans = 'pixels'
annotation('Arrow',[.2 .2],[.1 .2])
h=annotation('Arrow');
set(h,'Units','pixels','X',[300 200],'Y',[150 100])
thanks for your explanation,
In my case the results of pos are like:
pos=get(gca,'Position')
pos =
0.8651 0.1100 0.0399 0.8150
so I guess the first two values are x and y for start point of the figure, and the last two values are x and y for the end point of the figure,right? In order to better calculate the x and y for the arrow I want to know exactly where they come from , (sth/sth in the figure), so 0.8651 is the almost 86percent of the figure in x axis??
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')

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Asked:

on 13 Oct 2021

Commented:

on 14 Oct 2021

Community Treasure Hunt

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

Start Hunting!