MATLAB中annotation函数添加注释、箭头(归一化函数调用)

annotation函数(添加注释、箭头、文本框等)默认坐标为0-1的归一化坐标,这样操作很不方便。下面代码将axes的绝对坐标(即画图时的数据坐标转为0-1坐标,带入annotation即可),如下为归一化转化函数。

调用函数: 1. annotation('arrow',XY2Norm('X',[x_begin,x_end]),XY2Norm('Y',[y_begin,y_end]));

如:annotation('arrow',XY2Norm('X',[400,140]),XY2Norm('Y',[1400,1400]),  'LineStyle','-.','color','r','LineWidth',1.2);

2. annotation('arrow',x_to_norm_v2(x_begin,x_end),y_to_norm_v2(y_begin,y_end));

% 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
  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值