exc*_*aza 10
MATLAB具有内置annotation函数,可用于生成箭头并将其放置在绘图上.但是,MATLAB无意中编写了这个函数,使得xy输入被标准化为包含轴的图形窗口,而不是映射到轴中的数据点.这意味着我们需要转换它们,这是一项烦人的任务,但不是非常具有挑战性的任务.
我创建了一个带有一个箭头的小功能示例来说明该过程.这应该与任何xy轨迹兼容:
function testcode
h.myfig = figure();
h.myaxes = axes('Parent', h.myfig);
x = -10:10;
y = x.^2;
h.myplot = plot(h.myaxes, x, y);
for ii = 1:(length(x) - 1)
[newx, newy] = coordinate2normalized(h.myaxes, [x(ii) x(ii + 1)], [y(ii) y(ii + 1)]);
if exist('temp', 'var')
% No need to create another object if we have one, update existing one instead
set(temp, 'Units', 'Normalized');
temppos = get(temp, 'Position');
set(temp, 'X', newx);
set(temp, 'Y', newy);
else
temp = annotation('arrow', newx, newy);
end
pause(0.05)
end
end
function [xnorm, ynorm] = coordinate2normalized(axishandle, x, y)
set(axishandle, 'Units', 'Normalized');
axisposition = get(axishandle, 'Position'); % Get position in figure window
axislimits = axis(axishandle);
axisdatawidth = axislimits(2) - axislimits(1);
axisdataheight = axislimits(4) - axislimits(3);
% Normalize x position
xnorm = (x - axislimits(1))*(axisposition(3)/axisdatawidth) + axisposition(1);
% Normalize y position
ynorm = (y - axislimits(3))*(axisposition(4)/axisdataheight) + axisposition(2);
end
Run Code Online (Sandbox Code Playgroud)
产生以下内容:
当我有时间时,我打算至少稍微充实辅助函数,它将在GitHub上维护.