How to draw an arrow using non normalized coordinates?

418 visualizaciones (últimos 30 días)
Mr M.
Mr M. el 26 de Jun. de 2017
Editada: Bill Tubbs el 22 de Mzo. de 2023
I use: annotation('arrow',X,Y), and I tried to change units, but it is always normalized. How to use data units?
  3 comentarios
Mr M.
Mr M. el 28 de Jun. de 2017
yes, but I cannot figure out how to use non-normalized units with annotation('arrow')

Iniciar sesión para comentar.

Respuestas (9)

KarlHoff el 26 de Jun. de 2020
Editada: KarlHoff el 7 de Ag. de 2020
For me, using MATLAB R2018b,
the following works to produce an arrow at a location specified in data coordinates:
anArrow = annotation('arrow') ;
anArrow.Parent = gca; % or any other existing axes or figure
%EDIT thanks to @Moshe:
%anArrow.Position = [x_start, y_start, x_end, y_end] ;
anArrow.Position = [x_start, y_start, delta_x, delta_y] ;
The big advantage is that the arrow remains where it is with respect to other plot elements, even if the limits of the plot change afterwards.
This is not the case with all the approaches that convert data coordinates into other Units (Pixels, Inches, Normalized)
  3 comentarios
Jean-Michel el 2 de Sept. de 2020
Thanks for that , I have been fighting quiver and annotation to get similare result! it would be a nice addition to the docuementation

Iniciar sesión para comentar.

MICHAEL MUTWIRI el 21 de Ag. de 2021
Create the 'annotation' object for each subplot and edit their properties using dot notation.
For the X and Y properties you use similar values as your graph cordinates. The x-end and y_end are the tip of the arrow.
Below is a tested sample code
%Create sample data
x = linspace(0,2*pi,1e3);
y = sin(x); % Plotted of first subplot
z = cos(x); % Plotted of second subplot
% Specify different textarrows for different subplots
%%%**************** SUBPLOT 1 ********************
ylim([-1.1 1.1])
% Define X-Beginning and ending x-coordinates
x_start =pi-1;x_end = pi;
%Y- Beginning and ending y-coordinates
y_start =sin(x_end);y_end = sin(x_end);
anArrow = annotation('textarrow');
anArrow.Parent = gca;
anArrow.X = [x_start,x_end]; % set the x-property
anArrow.Y = [y_start ,y_end];
anArrow.String = 'sin(\pi)';
anArrow.Color = 'red';
%%%**************** SUBPLOT 2 ********************
ylabel('Amplitude ')
ylim([-1.1 1.1])
% Define X-Beginning and ending x-coordinates
x_start =1.5*pi-1;x_end = 1.5*pi;
%Y- Beginning and ending y-coordinates
y_start =cos(x_end);y_end = cos(x_end);
anArrow = annotation('textarrow');
anArrow.Parent = gca;
anArrow.X = [x_start,x_end]; % set the x-property
anArrow.Y = [y_start ,y_end];
anArrow.String = 'cos(3\pi/2)';
anArrow.Color = 'green';
  1 comentario
Bill Tubbs
Bill Tubbs el 22 de Mzo. de 2023
Editada: Bill Tubbs el 22 de Mzo. de 2023
Thanks. This works. Strange that you can't use axes co-ordinate system with the annotate command. It has a 'Units' argument but I can't find an appropriate value for this argument other than the default which is 'normalized' (where is the documentation?).

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 27 de Jun. de 2017
  8 comentarios
Timon Rayis
Timon Rayis el 11 de Nov. de 2019
Any developments with R2019b? annotations with non normalized coordinates like text ?
Adam Danz
Adam Danz el 11 de Nov. de 2019
Check out release notes for any updates on any release.

Iniciar sesión para comentar.

marcus yoder
marcus yoder el 23 de Ag. de 2018
Editada: marcus yoder el 23 de Ag. de 2018
I tested the code by Walter Robinson and had to make a few changes to get it to work.
function obj = dataArrow(Xdata,Ydata,ax)
%This function will draw an arrow on the plot for the specified data.
%The inputs are
oldunits = get(ax, 'Units');
set(ax, 'Units', 'Normalized');
axpos = ax.CurrentAxes.Position;
set(ax, 'Units', oldunits);
%get axes drawing area in data units
ax_xlim = ax.CurrentAxes.XLim;
ax_ylim = ax.CurrentAxes.YLim;
ax_per_xdata = axpos(3) ./ diff(ax_xlim);
ax_per_ydata = axpos(4) ./ diff(ax_ylim);
%these are figure-relative
Xpixels = (Xdata - ax_xlim(1)) .* ax_per_xdata + axpos(1);
Ypixels = (Ydata - ax_ylim(1)) .* ax_per_ydata + axpos(2);
obj = annotation('arrow', Xpixels, Ypixels, 'Units', 'pixels');

Robert el 7 de Sept. de 2019
I wanted to do something similar, here's an example that adds a double arrow between the x-values 1 and 5 with y-values 5 in a simple plot:

MichailM el 4 de Abr. de 2020
Maybe a function like the below could help. The x and y inputs are actual coordinates on the plot. Here I just need to draw an arrow but you can modify it
function myarrow(x,y)
ax = gca;
axpos = get(ax, 'Position');
X = get(gca,'XLim');
Y = get(gca,'YLim');
difX = X(2) - X(1);
difY = Y(2) - Y(1);
newx = x./difX;
newy = y./difY;
annotation('arrow',[newx(1)*axpos(3)+axpos(1) newx(2)*axpos(3)+axpos(1)],[newy(1)*axpos(4)+axpos(2) newy(2)*axpos(4)+axpos(2)])

Marc Compere
Marc Compere el 14 de Ag. de 2021
Scaling to achieve arrow annotations in axes units should be built into Matlab. The utility coord2norm() handles this easily.
This is a similar question with more discussions, but the short answer is: use coord2norm()
  1 comentario
Adam Danz
Adam Danz el 15 de Ag. de 2021
Thanks for pointing out that function, Marc, I'm sure it will be helpful in many cases.
Since it's a static, once-and-done, conversion, the annotation object may no longer be in the correct position if there are any changes to the figure size, axis size or position, axis limits, or aspect ratios. Calling the function after all plotting is complete would help to solve some of those issues. A more robust solution would be to assign listeners that update annotation objects when a resize or reposition event occurs but really what we need is for MathWorks to update the annotation function to support data units or offer users an alternative.

Iniciar sesión para comentar.

Vitaly Fedoseev
Vitaly Fedoseev el 26 de Mayo de 2021
The following code (Matlab R2019a) draws an arrow in the plot coordinates from point P1 to point P2. Zoom in/out shifts position of the arrow:
P1=[10,-1]; %from point
P2=[70,2]; % to point
Xlim=[-1 110];
Ylim=[-2 3];
Pos = [0.10 0.55 0.85 0.4];
subplot('Position', Pos)
hold on
x=0:0.1:100;plot(x, sin(x));plot([-100 1000], P2(2)*[1 1]); plot(P2(1)*[1 1], [-100 100]);
plot(x, sin(x));plot([-100 1000], P1(2)*[1 1]); plot(P1(1)*[1 1], [-100 100])
annotation('arrow', X_conv, Y_conv)
  3 comentarios
Vitaly Fedoseev
Vitaly Fedoseev el 1 de Jun. de 2021
Where should the line
be inserted in the code above?
Walter Roberson
Walter Roberson el 1 de Jun. de 2021
It should not be added to the code. Adam is saying that if you wanted to illustrate that the arrow did not follow the axes, then you could delete the axes and observe that the arrow is still there.

Iniciar sesión para comentar.

Bruce Jackson
Bruce Jackson el 26 de Oct. de 2022
It is absurd that we have to jump through hoops or download submitted code to plot an arrow in data units on a plot.
  2 comentarios
Adam Danz
Adam Danz el 27 de Oct. de 2022
Depending on what you're looking for, you can already plot simple arrows in data units using text().
x = rand(1,5);
y = rand(1,5);
text(x,y,repmat({char(8594)},size(x)), ...
'HorizontalAlignment', 'right', ...
'VerticalAlignment', 'middle', ...
'FontSize', 14)
Bruce Jackson
Bruce Jackson el 27 de Oct. de 2022
Thanks for the suggestion, but I would like to place and point arrows with a specified length and direction. The innovative use of UNICODE arrows unfortunately doesn't allow for more than four or maybe eight directions, I expect.
[The excellent coord2norm() function, written by user sco1, should be part of core MATLAB and not require me to download and install third-party code, which I'm attempting to avoid to have a marketable toolbox.]

Iniciar sesión para comentar.


Community Treasure Hunt

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

Start Hunting!

Translated by