在相空间肖像Matlab中添加箭头 [英] Add arrows in phase space portrait matlab

查看:116
本文介绍了在相空间肖像Matlab中添加箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB-如何添加在相位空间中遵循轨迹的恒定幅度的箭头(图像已附上图片)

MATLAB - How can one add arrows with constant magnitude that follow a trajectory in phase space ( image attached )

推荐答案

MATLAB具有内置的 annotation 函数,可用于生成箭头并将其放置在绘图中.但是,MATLAB以这种方式编写了该函数,以致xy输入被标准化为包含轴的图形窗口,而不映射到轴中的数据点.这意味着我们需要转换它们,这是一项烦人的任务,但不是一项具有挑战性的任务.

MATLAB has a built-in annotation function that can be used to generate arrows and place them on your plot. However, MATLAB unhelpfully has written this function in such a way that the xy inputs are normalized to the figure window containing the axes and not mapped to the data points in your axes. This means we need to convert them, an annoying task but not a very challenging one.

我创建了一个带有一个箭头的小功能示例,以说明该过程.这应该与任何xy轨迹兼容:

I have created a small functional example with one arrow to illustrate the process. This should be compatible with any xy trajectory:

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

会产生以下内容:

当我有时间的时候,我打算至少稍微完善一下辅助函数,并将其保留在 GitHub .

When I have time I intend to flesh out the helper function at least slightly, and it will be maintained on GitHub.

这篇关于在相空间肖像Matlab中添加箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆