有时候需要将x-y的位置绘制在屏幕中心或者其他位置,下面的代码可以实现原点移动到图中心.
% original data year = 1981:2011; a1 = 1.96 * ones(1,length(year)); a2 = -1.96 * ones(1,length(year)); % data plot(year,a1,'k',year,a2,'k'); % xlimit and y limit xlim([1981,2011]); ylim([-3,3]); % hold hold on; % get original ticks X = get(gca,'Xtick'); Y = get(gca,'Ytick'); % get original labels XL = get(gca,'XtickLabel'); YL = get(gca,'YtickLabel'); % new x-y limits X_Lim = get(gca,'XLim'); Y_Lim = get(gca,'YLim'); % new position of y-axis x_offset = diff(X_Lim)/2+X_Lim(1); % plot new x axis plot(X_Lim,[0 0],'k','linewidth',2); % plot new y axis plot([x_offset x_offset],Y_Lim,'k','linewidth',2); % small triangles ax = [X_Lim(end),X_Lim(end)-diff(X_Lim)/100,X_Lim(end)-diff(X_Lim)/100;0,diff(Y_Lim)/100,-diff(Y_Lim)/100]; ay = [x_offset,x_offset+diff(X_Lim)/120,x_offset-diff(X_Lim)/120;Y_Lim(end),Y_Lim(end)-diff(Y_Lim)/80,Y_Lim(end)-diff(Y_Lim)/80]; fill(ax(1,:),ax(2,:),'k'); fill(ay(1,:),ay(2,:),'k'); % get ticks offsets Xoff = diff(X_Lim)./80+x_offset; Yoff = diff(Y_Lim)./60; % draw new ticks for i=1:length(X) plot([X(i) X(i)],[0 Yoff],'-k'); end; for i=1:length(Y) plot([Xoff, x_offset],[Y(i) Y(i)],'-k'); end; % draw new tick labels text(X-1,zeros(size(X))-2.*Yoff,XL); text(Xoff+ones(size(Y))*0.5,Y,YL); % hide original axis axis off; % set white background set(gcf,'color','w');
No comments :
Post a Comment