18
votes

In Matlab figure, I would like to remove ticks only from the top and right axes with keeping the plot box on.

I know if I make the plot box off, the ticks on the top and right go away. But, this is not what I want. In other words, I want to keep ticks only at the bottom and left and, at the same time, want to keep the plot box on.

2
This one of those things that you simply can't do nicely. You have to resort to trickery. If you are trying to get two axes on one another with linked x-axis and two separate y-axes on both sides, the easiest approach is to set both axes to box off. Then move x-axe of second axes to the top, remove tick and axis labels and it will nicely close the image.j_kubik
That worked. Thanks! I couldn't edit the code nicely here, but I put the code below. figure lw = 2; x=0:5:10; plot(x,x) a1 = gca; set(a1,'box','off','tickdir','out','xticklabel',{},'yticklabel',{},... 'linewidth',lw,'Xtick',[0:5:10],'ytick',[0:5:10]) axis square a2 = copyobj(a1,gcf); set(a2,'color','none','xaxislocation','top','yaxislocation','right','xtick',[],'ytick',[])Daisuke Takeshita

2 Answers

12
votes

My workaround similar to @j_kubik proposition:

plot(1:10)
% get handle to current axes
a = gca;
% set box property to off and remove background color
set(a,'box','off','color','none')
% create new, empty axes with box but without ticks
b = axes('Position',get(a,'Position'),'box','on','xtick',[],'ytick',[]);
% set original axes as active
axes(a)
% link axes in case of zooming
linkaxes([a b])
0
votes

You can use box off to remove the ticks and then draw the box back using plot. For example:

figure
hold on
box off
plot(1:10)
plot([1,10],[10, 10],'k')
plot([10,10],[1,10],'k')