R2016b Matlab把坐标轴放到坐标原点简单多了
- 之前在博客里发过一个Matlab把坐标轴移动到坐标原点的文章。今天偶然发现新版本R2016b以后再想把坐标轴放到原点,比原来的方法简单得多了。
- 先给一个官方的链接 from www.mathworks.com
Display Axis Lines Through Origin
讲起来之所以变简单了,是因为对于图像句柄,新增了两个属性
- XAxisLocation: 存在三个值选项 ‘top’ ‘bottom’ ‘origin’
- YAxisLocation: 存在三个值选项 ‘left’ ‘right’ ‘origin’
下面把官网的例子借用一下:
x = linspace(-10,4);
y = sin(x);
plot(x,y)
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
很简单的句柄操作方法
ax.Box = 'off';
官网的例子就到这里为止了,通常我们还需要对它加一点点修饰
套用之前那个例子画箭头的方式
pos = get(gca,'Position');
x_Lim = get(gca,'Xlim');
y_Lim = get(gca,'Ylim');
if prod(y_Lim)>0
position_x = [pos(1), pos(2)+pos(4)/2, pos(3), eps];
else
position_x = [pos(1), pos(2)-y_Lim(1)/diff(y_Lim)*pos(4), pos(3), eps];
end
if prod(x_Lim)>0
position_y = [pos(1)+pos(3)/2, pos(2), eps, pos(4)];
else
position_y = [pos(1)-x_Lim(1)/diff(x_Lim)*pos(3), pos(2), eps, pos(4)];
end
annotation('arrow', [pos(1)-0.065*pos(3), pos(1)+pos(3)+0.065*pos(3)], ...
[position_x(2)-0.001,position_x(2)-0.001],'HeadLength',6,'HeadWidth',6);
annotation('arrow', [position_y(1)+0.001, position_y(1)+0.001],...
[pos(2)-0.065*pos(4),pos(2)+pos(4)+0.065*pos(4)],...
'HeadLength',6,'HeadWidth',6);