Remove only axis lines without affecting ticks and tick labels

冷暖自知 提交于 2019-12-17 18:31:39

问题


Is there a way to remove only the axis lines in the Matlab figure, without affecting ticks and tick labels.

I know that box toggles the upper and right axes lines and ticks and that works perfectly for me.
But my problem is that I want eliminate the bottom and left lines (only lines!) but keeping the ticks and tick labels.

Any tricks?


回答1:


There is another undocumented way (applicable to MATLAB R2014b and later versions) of removing the lines by changing the 'LineStyle' of rulers to 'none'.

Example:

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'


This is different from Dan's answer which uses the 'visible' property of rulers.




回答2:


Yair Altman's Undocumented Matlab demonstrates a cleaner way to do this using the undocumented axes rulers:

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;

A nice feature of this approach is that you can separately format the x and y axis lines.




回答3:


Solution for Matlab versions prior to R2014b

You can introduce a new white bounding box and put it on top.

// 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])

Important is to deactivate the ticks of the second axes, to keep the ticks of the f rist one.

In Luis Mendo's solution, the plotted lines are fixed and stay at their initial position if you change the axes properties afterwards. That won't happen here, they get adjusted to the new limits. Use the correct handle for every command and there won't be much problems.

Dan's solution is easier, but does not apply for Matlab versions before R2014b.




回答4:


You could "erase" the axis lines by plotting a white line over them:

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

Result:


As noted by @SardarUsama, in recent Matlab versions you may need to adjust the line width to cover the axes:

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); 


来源:https://stackoverflow.com/questions/25838789/remove-only-axis-lines-without-affecting-ticks-and-tick-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!