Matlab怎样移动y轴?

img

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

用一下这个函数就行了

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

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632