首页 > 其他分享 >matlab将坐标轴转换为箭头

matlab将坐标轴转换为箭头

时间:2023-04-29 10:24:10浏览次数:33  
标签:UserData end pos 箭头 坐标轴 isempty matlab arrow ax

先将原本的坐标轴轴线关闭

ax=gca;
ax.YAxis.Visible = 'off';
ax.XAxis.Visible = 'off';
ax.ZAxis.Visible = 'off';

然后调用函数

ax.LineWidth=1.2;
ax.XAxisLocation='origin';
arrowAxes(ax)

函数如下:ax是轴gca的属性

function arrowAxes(ax)

if nargin<1
    ax=gca;
end

ax.Box='off';
ax.UserData.arrow{1}=[];
ax.UserData.arrow{2}=[];
ax.UserData.arrow{3}=[];
ax.UserData.arrow{4}=[];

pos=ax.Position;
xm=.02;
ym=.02;
% -------------------------------------------------------------------------
switch ax.XAxisLocation
    case 'bottom'
        ax.UserData.arrow{2}=annotation('arrow');
        ax.UserData.arrow{2}.Color=ax.YColor;
        ax.UserData.arrow{2}.Position=[pos(1),pos(2),0,pos(4)+ym];
    case 'top'
        ax.UserData.arrow{4}=annotation('arrow');
        ax.UserData.arrow{4}.Color=ax.YColor;
        ax.UserData.arrow{4}.Position=[pos(1),pos(2)+pos(4),0,-pos(4)-ym];
    case 'origin'
        ax.UserData.arrow{2}=annotation('arrow');
        ax.UserData.arrow{2}.Color=ax.YColor;
        ax.UserData.arrow{2}.Position=[pos(1),pos(2),0,pos(4)+ym];
        ax.UserData.arrow{4}=annotation('arrow');
        ax.UserData.arrow{4}.Color=ax.YColor;
        ax.UserData.arrow{4}.Position=[pos(1),pos(2)+pos(4),0,-pos(4)-ym];
end
switch ax.YAxisLocation
    case 'left'
        ax.UserData.arrow{1}=annotation('arrow');
        ax.UserData.arrow{1}.Color=ax.XColor;
        ax.UserData.arrow{1}.Position=[pos(1),pos(2),pos(3)+xm,0];
    case 'right'
        ax.UserData.arrow{3}=annotation('arrow');
        ax.UserData.arrow{3}.Color=ax.XColor;
        ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2),-pos(3)-xm,0];
    case 'origin'
        ax.UserData.arrow{1}=annotation('arrow');
        ax.UserData.arrow{1}.Color=ax.XColor;
        ax.UserData.arrow{1}.Position=[pos(1),pos(2),pos(3)+xm,0];
        ax.UserData.arrow{3}=annotation('arrow');
        ax.UserData.arrow{3}.Color=ax.XColor;
        ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2),-pos(3)-xm,0];
end

if strcmp(ax.XAxisLocation,'top')
    if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2)+pos(4),pos(3)+xm,0];end
    if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2)+pos(4),-pos(3)-xm,0];end
end
if strcmp(ax.YAxisLocation,'right')
    if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1)+pos(3),pos(2),0,pos(4)+ym];end
    if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1)+pos(3),pos(2)+pos(4),0,-pos(4)-ym];end
end
for i=1:4
    if ~isempty(ax.UserData.arrow{i}),ax.UserData.arrow{i}.LineWidth=ax.LineWidth;end
end


reArrow()
% -------------------------------------------------------------------------
function reArrow(~,~)
if strcmp(ax.XAxisLocation,'origin')
    pos=ax.Position;
    ylim=ax.YLim;
    sepy=(0-ylim(1))./(ylim(2)-ylim(1)).*pos(4);
    switch true
        case ylim(2)<=0
            if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2)+pos(4),pos(3)+xm,0];end
            if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2)+pos(4),-pos(3)-xm,0];end
        case ylim(1)>=0
            if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2),pos(3)+xm,0];end
            if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2),-pos(3)-xm,0];end
        case ylim(2)>0&ylim(1)<0
            if ~isempty(ax.UserData.arrow{1}),ax.UserData.arrow{1}.Position=[pos(1),pos(2)+sepy,pos(3)+xm,0];end
            if ~isempty(ax.UserData.arrow{3}),ax.UserData.arrow{3}.Position=[pos(1)+pos(3),pos(2)+sepy,-pos(3)-xm,0];end
    end
end
if strcmp(ax.YAxisLocation,'origin')
    pos=ax.Position;
    xlim=ax.XLim;
    sepx=(0-xlim(1))./(xlim(2)-xlim(1)).*pos(3);
    switch true
        case xlim(2)<=0
            if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1)+pos(3),pos(2),0,pos(4)+ym];end
            if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1)+pos(3),pos(2)+pos(4),0,-pos(4)-ym];end
        case xlim(1)>=0
            if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1),pos(2),0,pos(4)+ym];end
            if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1),pos(2)+pos(4),0,-pos(4)-ym];end
        case xlim(2)>0&xlim(1)<0
            if ~isempty(ax.UserData.arrow{2}),ax.UserData.arrow{2}.Position=[pos(1)+sepx,pos(2),0,pos(4)+ym];end
            if ~isempty(ax.UserData.arrow{4}),ax.UserData.arrow{4}.Position=[pos(1)+sepx,pos(2)+pos(4),0,-pos(4)-ym];end
    end
end
end
set(ax.Parent,'WindowButtonMotionFcn',@reArrow);  % 设置鼠标按下回调
end

标签:UserData,end,pos,箭头,坐标轴,isempty,matlab,arrow,ax
From: https://www.cnblogs.com/Ada-CN/p/17363643.html

相关文章

  • matlab设置坐标轴颜色以及标签换行显示、旋转显示
    set(gca,'ycolor',[000]);%颜色%设置不同字体、字型、以及换行显示,换行显示用{}包住每行字即可yr=ylabel('\it\fontname{TimesNewRoman}y_{CS}^{M4}...\rm\fontname{TimesNewRoman}\cdot{10}');%旋转跳跃set(yr,'Rotation',0,'position',[5.71100]......
  • matlab双y轴
    clcclearcloseallSita=0:0.25:5;y1=-20000./(1.6.*Sita.^2-288.00);y2=-20000./(1.2.*Sita.^2-208.00);y3=-56000./(1.92.*Sita.^2-294.400);y4=-80000./(0.92.*Sita.^2-64.00);figure;holdonset(gcf,'Position',[176,193,621,526]);%......
  • matlab读取文件中时间格式变量并将x轴以时间格式显示
    clcclearcloseall%读取数据文件[num,txt,raw]=xlsread('data.xlsx');%将第一列读进来作为时间dateStrings=raw(2:end,1);%将第一列设置为时间数字格式x_raw=datenum(dateStrings);x=x_raw(:,:);%绘制图像figureholdon%设置图片位置大小set(gcf,'Posit......
  • 示波器数据导入MATLAB进行FFT分析的方法
      http://blog.sina.com.cn/s/blog_710421fa0101crm1.htmlpower_fftscope;示波器保存为.csv格式文件,然后用matlab导入新建.mdl模型文件,示波器里面变量保存为uuuu.time=seconduu.signals.values=Volt在工作台运行上面两条指令,直到FFT分析几面里面出现波形,就可以分析了!1......
  • matlab生成点云
    clc;clearall;closeall;N=1000;%#NumberofpointsV=[-10,0;0,10;10,0];%#Trianglevertices,pairsof(x,y)t=sqrt(rand(N,1));s=rand(N,1);P=(1-t)*V(1,:)+bsxfun(@times,((1-s)*V(2,:)+s*V(3,:)),t);u......
  • 基于肤色空间建模+连通域处理的人脸检测算法的MATLAB仿真
    1.算法仿真效果matlab2022a仿真结果如下:   2.算法涉及理论知识概要        在过去的几年里,人脸识别受到了广泛的关注,被认为是图像分析领域最有前途的应用之一。人脸检测可以考虑人脸识别操作的很大一部分。根据其强度将计算资源集中在持有人脸的图像部分。图片......
  • m基于模糊控制的网络时延预测和丢包率matlab仿真
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要涉及到具体的数据包大小以及时间延迟,我们通过构建一个FIFO,来虚拟网络的实际工作情况,当空闲情况下,网络流量非常小,我们的数据通过FIFO,会在FIFO内排队,等候前面的数据传输出去后,再发送出去,在FIFO中等候时间为30~60ms,小......
  • 基于肤色空间建模+连通域处理的人脸检测算法的MATLAB仿真
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要在过去的几年里,人脸识别受到了广泛的关注,被认为是图像分析领域最有前途的应用之一。人脸检测可以考虑人脸识别操作的很大一部分。根据其强度将计算资源集中在持有人脸的图像部分。图片中的人脸检测方法很复杂,因为......
  • m基于背景差法与GMM混合高斯模型结合的红外目标检测与跟踪算法matlab仿真
    1.算法仿真效果matlab2013b仿真结果如下: 普通视频:  红外视频:   2.算法涉及理论知识概要       在Stauffer等人提出的自适应混合高斯背景模型基础上,为每个像素构建混合高斯背景模型,通过融入帧间差分把每帧中的图像区分为背景区域、背景显露区域和运动物......
  • m基于模糊控制的网络时延预测和丢包率matlab仿真
    1.算法仿真效果matlab2022a仿真结果如下:    2.算法涉及理论知识概要        涉及到具体的数据包大小以及时间延迟,我们通过构建一个FIFO,来虚拟网络的实际工作情况,当空闲情况下,网络流量非常小,我们的数据通过FIFO,会在FIFO内排队,等候前面的数据传输出去后,再发......