matlab - set tick label at origin
在用Matlab生成图时,当x和y的最小值都为零时,我宁愿只用一个零来标记原点,而不是在两个轴上都标出。
Matlab默认为后者,就像这样
而我想要更多这样的东西
这可以手动完成,但是我正在尝试使过程自动化。 删除
有任何想法吗?
我正在运行Matlab 2014b。
这是一种简单的方法:
1 2 3 4 5 6 7 8 9 10 11 |
结果:
如果您希望
1 2 3 4 5 6 7 8 | % get the position of the zero tick on x-axis: zeroXtick = ax.XAxis.TickValues==0; % remove it (tick and lable) ax.XAxis.TickValues(zeroXtick) = []; % place a new zero at the origin: dim = [0.1 0.01 0.1 0.1]; annotation('textbox',dim,'String','0',... 'FitBoxToText','on','LineStyle','none') |
您将获得:
编辑:
对于2016a之前的版本,您可以使用以下代码(我写得更紧凑一些):
1 2 3 4 5 6 7 8 9 | ax = gca; % remove the zero tick on y-axis (tick and lable): ax.YTick(ax.YTick==0) = []; % remove the zero tick on x-axis (tick and lable): ax.XTick(ax.XTick==0) = []; % place a new zero at the origin: dim = [0.1 0.01 0.1 0.1]; annotation('textbox',dim,'String','0',... 'FitBoxToText','on','LineStyle','none') |
编辑2:
保持
1 2 | t = annotation('textbox',dim,'String','0',... 'FitBoxToText','on','LineStyle','none'); |
然后切换
1 2 | t.Parent = ax; t.Position(1:2) = -[0.2 0.1]; |
最后,我们通过将
1 | t.Units = 'Pixels'; |
下面将不计入yaxis的第一个刻度。您可以通过在删除第一个轴限制之前设置轴限制