『ßengi』 2022-03-02 21:32 采纳率: 100%
浏览 36
已结题

Matlab怎样移动y轴?

img

如图,我想将Y轴移动到x=2的位置,翻了官方文档还是找不到怎样做,谢谢了!

  • 写回答

2条回答 默认 最新

  • 技术专家团-Joel 2022-03-03 09:22
    关注

    用一下这个函数就行了

    function axescenter(ax)
    if nargin<1
        ax = gca;
    elseif ~ishandle(ax) || isempty(strmatch('axes',get(ax,'type')))
        error('Input argument must be the handle to an axes.  See help.')
    end
    if any(strmatch('log',get(gca,{'xsca','ysca'}))) || all(get(ax,'view'))
        error('AXESCENTER does not work for log scaled  or 3D axes.  See help')
    end
    D = get(ax,{'pos','xlim','ylim','units','parent'}); %Set these props first.
    fc = get(gcf,'color'); % We are going to hide ax in the figure.
    set(ax,'tickd','out',{'xcolor';'ycolor'},{fc,fc});
    H = get(ax,{'xlabel','ylabel','title'}); % Don't want to hide these guys.
    set([H{:}],{'color'},{'k'})
    % Next create our fake axes that will act as the center axes.    
    S.a1 = axes('units',D{4},'posi',[D{1}(1)+D{1}(3)/2 D{1}(2) .01 D{1}(4)],...
         'ylim',D{3},'xtick',[],'handlev','of','tag','XAXIS','color','none',...
         'hittest','off','parent',D{5});  % Give this fake axis the props.
    S.a2 = axes('units',D{4},'posi',[D{1}(1) D{1}(2)+D{1}(4)/2 D{1}(3) .01],...
         'xlim',D{2},'ytick',[],'handlev','of','tag','YAXIS','color','none',...
         'hittest','off','parent',D{5});  % Give this fake axis the props.
    setappdata(ax,'CENTERAXES',S); % Store the handles in appdata of AX.
    
    
    

    示例:

    x = rand(20,1)*4;
    y = rand(20,1)*3 - 1.5;
    plot(x,y, 'bo', 'markerfacecolor', 'b')
    set(gca, 'xlim', [0, 4], 'ylim',[-1.5, 1.5]) % 注意是区间的中间为坐标轴位置
    grid on
    axescenter
    
    

    效果

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月2日

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭