Annotating an Arrow with in a plot.

16 views (last 30 days)
Pappu Murthy
Pappu Murthy on 2 Mar 2018
Answered: Nirjhar Kumar on 2 Apr 2019
I am trying to use the annotation function to show an arrow on my plot like ar = annotation('arrow'..) I know the location where I want the arrow to start the x and y coordinates in my data units. However, the documentation says I can only specify in normalized units which are between 0 and 1 for any figure. But this will not work for me. So what is a work around so that I can put the arrow exactly at a point specified by userdata.
  1 Comment
Pappu Murthy
Pappu Murthy on 4 Mar 2018
When I first create the figure it looks like this: picture file one.png
I click on plot tools icon it then fills my screen and i see it full size but my arrows are all badly placed now.

Sign in to comment.

Accepted Answer

Jan
Jan on 2 Mar 2018
Edited: Jan on 2 Mar 2018
  1 Comment
Pappu Murthy
Pappu Murthy on 5 Mar 2018
I tried the FEX function it works fine for me. But again When I change the plots to fullscreen mode with plottools on the arrows get displaced badly.

Sign in to comment.

More Answers (2)

Pappu Murthy
Pappu Murthy on 4 Mar 2018
the link helped me solve the problem partially. When I click on maximize the window with plot tools, all the arrow positions are screwed up. Or if I place the arrows after I adjust the figure to fit the window and click on plot tools, then the arrows are placed correctly. However, I have no control to make the figure full screen and with plot tools enabled without manually clicking the options. How do i overcome this?
  3 Comments
Pappu Murthy
Pappu Murthy on 5 Mar 2018
Some nothing seems to work ok with my specific case. If I create a sample case it seems to work. So what I finally tried after searching through Matlab helpfile is to include a line "plottools('on') before I create my 'arrows' and it worked like charm. So I am going to close this question. Thanks for all the help provided. They did solve most of the problem if not 100%.

Sign in to comment.


Nirjhar Kumar
Nirjhar Kumar on 2 Apr 2019
open a figure and Just pass the position of x data's to the below function
function obj = dataArrow2(xpos)
%This function will draw an arrow on the plot for the specified x data position .
%Eg:
% plot(0:0.001:10,sin(0:0.001:10))
% dataArrow2([3000 6000 9000])
handles=findall(0,'type','figure');
for fig_cnt= 1:1:length(handles)
figure(handles(fig_cnt,1))
ax=handles(fig_cnt,1);
% hLeg=findobj(handles(fig_cnt,1),'type','legend');
% set(hLeg,'visible','off')
axObjs=findobj(handles(1,1),'type','Axes');%axObjs = ax.Children;
dataObjs = axObjs.Children;
oldunits = get(axObjs, 'Units');
set(axObjs, 'Units', 'Inches');
axpos = axObjs.Position
set(ax, 'Units', oldunits);
oldunits = get(handles, 'Units');
set(handles, 'Units', 'Inches');
figpos= handles.Position
set(handles, 'Units', oldunits);
% set(hLeg,'visible','off')
%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);
for i=1:1:length(dataObjs)
Xdata = dataObjs(i).XData;
Ydata = dataObjs(i).YData;
%these are figure-relative
for j=1:1:length(xpos)
Xpixels = (Xdata([xpos(j) xpos(j)+1]) - ax_xlim(1)).* ax_per_xdata+axpos(1);
%if set(gca,'xdir','reverse') is used
%Xpixels = -(Xdata([pos pos+1]) - ax_xlim(1)).* ax_per_xdata+axpos(3)+axpos(1);
Ypixels = (Ydata([xpos(j) xpos(j)+1]) - ax_ylim(1)).* ax_per_ydata+axpos(2);
%if set(gca,'ydir','reverse') is used
%Ypixels = -(Ydata([pos pos+1]) - ax_ylim(1)).* ax_per_ydata+axpos(2)+axpos(4);
annotation('arrow', Xpixels/figpos(3), Ypixels/figpos(4));
end;
obj = 1;
end
end;

Community Treasure Hunt

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

Start Hunting!

Translated by