Annotating an Arrow with in a plot.
조회 수: 12 (최근 30일)
이전 댓글 표시
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.
채택된 답변
추가 답변 (2개)
Nirjhar Kumar
2019년 4월 2일
Adapted marcus yoder code at https://in.mathworks.com/matlabcentral/answers/346297-how-to-draw-an-arrow-using-non-normalized-coordinates .
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;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!