annotation函数(添加注释、箭头、文本框等)默认坐标为0-1的归一化坐标,这样操作很不方便。下面代码将axes的绝对坐标(即画图时的数据坐标转为0-1坐标,带入annotation即可)
% https://ww2.mathworks.cn/matlabcentral/fileexchange/8269-transform-axes-units-to-normalized-units-for-annotation-objects
%author: Girish Ratanpal, ECE, UVa.
%transform axes units to normalized units for 2-D figures only.
%works for linear,log scales and also reverse axes.
%DATE: JAN. 6, 2006. previous version: aug 12, 2005.
%
%
%FUNCTION DESCRIPTION:
% function [y_norm] = y_to_norm_v2(y_begin,y_end)
% function returns a 1x2 array, y_norm, with normalized unitsy_begin and
% y_end for the annotation object.
%
% function arguments:
%1. y_begin: enter the point where the annotation object begins on the axis
% using axis units
%2. y_end: end of annotation object, again in axis units.
%
%EXAMPLE: first plot the graph on the figure.
%then use the following command for placing an arrow:
%h_object =
%annotation('arrow',x_to_norm_v2(x_begin,x_end),y_to_norm_v2(y_begin,y_end));
%******************************************
%CODE FOR x_norm_v2() IS COMMENTED AT THE END.
%******************************************
function [norm] = XY2Norm(XY,P)
% annotation('arrow',XY2Norm('X',[x_begin,x_end]),XY2Norm('Y',[y_begin,y_end]));
if strcmp(XY,'X')
norm = x_to_norm_v2(P(1),P(2));
elseif strcmp(XY,'Y')
norm = y_to_norm_v2(P(1),P(2));
end
function [y_norm] = y_to_norm_v2(y_begin,y_end)
if nargin ~= 2
error('Wrong number of input arguments! y_to_norm_v2(y_begin,y_end)')
end
h_axes = get(gcf,'CurrentAxes'); %get axes handle.
axesoffsets = get(h_axes,'Position');%get axes position on the figure.
y_axislimits = get(gca, 'ylim'); %get axes extremeties.
y_dir = get(gca,'ydir');
y_scale = get(gca,'YScale');
%get axes length
y_axislength_lin = abs(y_axislimits(2) - y_axislimits(1));
if strcmp(y_dir,'normal') %axis not reversed
if strcmp(y_scale,'log')
%get axes length in log scale.
y_axislength_log = abs(log10(y_axislimits(2)) - log10(y_axislimits(1)));
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_begin)-log10(y_axislimits(1)))/(y_axislength_log);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_end)-log10(y_axislimits(1)))/(y_axislength_log);
y_norm = [y_begin_norm y_end_norm];
elseif strcmp(y_scale,'linear')%linear scale.
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs((y_begin-y_axislimits(1))/y_axislength_lin);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs((y_end-y_axislimits(1))/y_axislength_lin);
y_norm = [y_begin_norm y_end_norm];
else
error('use only lin or log in quotes for scale')
end
elseif strcmp(ydir,'reverse') %axis is reversed
if strcmp(y_scale,'log')
%get axes length in log scale.
y_axislength_log = abs(log10(y_axislimits(2)) - log10(y_axislimits(1)));
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_axislimits(2))-log10(y_begin))/(y_axislength_log);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_axislimits(2))-log10(y_end))/(y_axislength_log);
y_norm = [y_begin_norm y_end_norm];
elseif strcmp(y_scale,'linear')
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs((y_axislimits(2)-y_begin)/y_axislength_lin);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs((y_axislimits(2)-y_end)/y_axislength_lin);
y_norm = [y_begin_norm y_end_norm];
else
error('use only lin or log in quotes for scale')
end
else
error('use only r or nr in quotes for reverse')
end
%********************************************************
function [x_norm] = x_to_norm_v2(x_begin,x_end)
if nargin ~= 2
error('Wrong number of input arguments.')
end
h_axes = get(gcf,'CurrentAxes'); %get axes handle
axesoffsets = get(h_axes,'Position');
x_axislimits = get(gca, 'xlim'); %get axes extremeties.
x_dir = get(gca,'xdir');
x_scale = get(gca,'xscale');
%get axes length
x_axislength_lin = abs(x_axislimits(2) - x_axislimits(1));
if strcmp(x_dir,'normal')
if strcmp(x_scale,'log')
x_axislength_log = abs(log10(x_axislimits(2)) - log10(x_axislimits(1)));
x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_begin)-log10(x_axislimits(1)))/(x_axislength_log);
x_end_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_end)-log10(x_axislimits(1)))/(x_axislength_log);
x_norm = [x_begin_norm x_end_norm];
elseif strcmp(x_scale,'linear')
x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs((x_begin-x_axislimits(1))/x_axislength_lin);
x_end_norm = axesoffsets(1)+axesoffsets(3)*abs((x_end-x_axislimits(1))/x_axislength_lin);
x_norm = [x_begin_norm x_end_norm];
else
error('use only lin or log in quotes for scale')
end
elseif strcmp(x_dir,'reverse')
if strcmp(x_scale,'log')
x_axislength_log = abs(log10(x_axislimits(2)) - log10(x_axislimits(1)));
x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_axislimits(2))-log10(x_begin))/(x_axislength_log);
x_end_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_axislimits(2))-log10(x_end))/(x_axislength_log);
x_norm = [x_begin_norm x_end_norm];
elseif strcmp(x_scale,'linear')
x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs((x_axislimits(2)-x_begin)/x_axislength_lin);
x_end_norm = axesoffsets(1)+axesoffsets(3)*abs((x_axislimits(2)-x_end)/x_axislength_lin);
x_norm = [x_begin_norm x_end_norm];
else
error('use only lin or log in quotes for scale')
end
else
error('use only r or nr in quotes for reverse')
end