Remove only axis lines without affecting ticks and tick labels

Denny Alappatt picture Denny Alappatt · Sep 15, 2014 · Viewed 9.5k times · Source

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?

Answer

thewaywewalk picture thewaywewalk · Sep 15, 2014

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.

enter image description here

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.