19

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.

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
Daisuke Takeshita
  • 195
  • 1
  • 1
  • 5
  • 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 Jun 20 '13 at 16:34
  • 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 Jun 21 '13 at 11:50

3 Answers3

12

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])
Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
mc2
  • 393
  • 6
  • 15
  • I didn't know about linkaxes. It's an improvement. Thanks!! – Daisuke Takeshita Nov 21 '13 at 07:02
  • Update: for users of Matlab 2014 and later, replace `linkaxes` with `linkprop` – Carl Witthoft Jun 27 '16 at 12:14
  • @CarlWitthoft I think `linkaxes` is still valid in 2014+. Calling `linkprop([a b])` will cause an error – Delyle Nov 02 '16 at 22:00
  • @Delyle yes it's still valid, but as the Help page says, "See the linkprop function for more advanced capabilities that allow you to link object properties on any graphics object" . Yes, the arg list is slightly different (duh) :-) – Carl Witthoft Nov 03 '16 at 11:12
0

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')
Molly
  • 13,240
  • 4
  • 44
  • 45
  • Thanks for the suggestion. I tried that. But, it doesn't work as nicely as I wish. When I set the linewidth thicker, say 3, then the lines drawn on top and right (the ones drawn with plot) look thinner than axes (lines with left and bottom), even though I use the same line width for the box drawn with plot and axes. – Daisuke Takeshita Mar 22 '13 at 10:39
0

Now in 2022, if anyone is still interested in a quick resolution other than the box option, here is my answer:

figure  
plot(1:10) ;  
ax = gca ;  
ax.Box = 'off'  ;  
xline(ax.XLim(2),'-k', 'linewidth',ax.LineWidth);  
yline(ax.YLim(1),'-k', 'linewidth',ax.LineWidth);