有没有一种方法可以在Matlab图中仅删除轴线,而不影响刻度和刻度标签。
我知道
有什么诀窍吗?
我知道
box
可以切换上部和右部的轴线和刻度,这对我来说非常完美。
但我的问题是,我想要消除底部和左侧线(只有线!),同时保留刻度和刻度标签。有什么诀窍吗?
box
可以切换上部和右部的轴线和刻度,这对我来说非常完美。
但我的问题是,我想要消除底部和左侧线(只有线!),同时保留刻度和刻度标签。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;
您可以引入一个新的白色边框并将其放在最上面。
// 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])
重要的是要停用第二个轴的刻度,保留第一个轴的刻度。
有另一种未记录的方法(适用于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'
这与丹的回答不同,后者使用标尺的'visible'属性。
plot(1:4,1:4) %// example plot
box off %// remove outer border
hold on
a = axis; %// get axis size
plot([a(1) a(2)],[a(3) a(3)],'w'); %// plot white line over x axis
plot([a(1) a(1)],[a(3) a(4)],'w'); %// plot white line over y axis
结果:
正如@SardarUsama所指出的,在最近的Matlab版本中,您可能需要调整线宽以覆盖坐标轴:
plot(1:4,1:4) %// example plot
box off %// remove outer border
hold on
a = axis; %// get axis size
plot([a(1) a(2)],[a(3) a(3)],'w', 'linewidth', 1.5); %// plot white line over x axis.
%// Set width manually
plot([a(1) a(1)],[a(3) a(4)],'w', 'linewidth', 1.5);
1.5
似乎可以正常工作(尽管这可能取决于系统)。我已经更新了回答。 - Luis Mendo