仅删除轴线而不影响刻度和刻度标签

Den*_*att 7 matlab plot axis line matlab-figure

有没有办法只删除Matlab图中的轴线,而不影响刻度和刻度标签.

我知道box切换上轴和右轴的线和刻度,这对我来说非常有效.
但我的问题是我想要消除底线和左线(只有线!)但保留刻度和刻度标签.

任何招数?

the*_*alk 8

R2014b之前的Matlab版本的解决方案

您可以引入一个新的白色边框并将其放在顶部.

// example data
x = linspace(-4,4,100);
y = 16 - x.^2;

plot(x,y); hold on
ax1 = gca;
set(ax1,'box','off')  %// here you can basically decide whether you like ticks on
                      %// top and on the right side or not

%// new white bounding box on top
ax2 = axes('Position', get(ax1, 'Position'),'Color','none');
set(ax2,'XTick',[],'YTick',[],'XColor','w','YColor','w','box','on','layer','top')

%// you can plot more afterwards and it doesn't effect the white box.
plot(ax1,x,-y); hold on
ylim(ax1,[-30,30])
Run Code Online (Sandbox Code Playgroud)

重要的是停用第二个轴的刻度,以保持第一个轴的刻度.

在此输入图像描述

Luis Mendo的解决方案中,如果之后更改轴属性,则绘制的线条将固定并保持在其初始位置.这不会发生在这里,他们会调整到新的限制.为每个命令使用正确的句柄,不会有太多问题.

Dan的解决方案更容易,但不适用于R2014b之前的Matlab版本.


Dan*_*Dan 8

Yair Altman的Undocumented Matlab使用未记录的轴标尺演示了一种更简洁的方法:

plot(x,y);
ax1 = gca;
yruler = ax1.YRuler;
yruler.Axle.Visible = 'off';
xruler = ax1.XRuler;
xruler.Axle.Visible = 'off'; %// note you can do different formatting too such as xruler.Axle.LineWidth = 1.5;
Run Code Online (Sandbox Code Playgroud)

这种方法的一个很好的特点是你可以单独格式化x和y轴线.


Sar*_*ama 6

还有另一种未记录的方法(适用于 MATLAB R2014b 和更高版本)通过将'LineStyle'标尺的 更改为来删除线条'none'

例子:

figure;
plot(1:4,'o-');  %Plotting some data
pause(0.1);      %Just to make sure that the plot is made before the next step
hAxes = gca;     %Axis handle
%Changing 'LineStyle' to 'none'
hAxes.XRuler.Axle.LineStyle = 'none';  
hAxes.YRuler.Axle.LineStyle = 'none';
%Default 'LineStyle': 'solid', Other possibilities: 'dashed', 'dotted', 'dashdot'
Run Code Online (Sandbox Code Playgroud)

输出


这与Dan使用统治者的“可见”属性的答案不同。