Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB remove ticks on one axis while keeping labels

I want to make a MATLAB plot that has tick labels but no tick marks on the x axis, but does have tick marks on the y axis. How can I do this?

I can't use

set(gca,'XTick',[])

because this would remove the tick labels. I also can't use

set(gca,'TickLength',[0 0])

because this would remove tick marks on the y axis.

like image 712
Max Radin Avatar asked Oct 09 '14 21:10

Max Radin


People also ask

How do you remove an axis tick?

To remove the ticks on both the x-axis and y-axis simultaneously, we can pass both left and right attributes simultaneously setting its value to False and pass it as a parameter inside the tick_params() function. It removes the ticks on both x-axis and the y-axis simultaneously.

How do you control Xticks in Matlab?

xticks( ticks ) sets the x-axis tick values, which are the locations along the x-axis where the tick marks appear. Specify ticks as a vector of increasing values; for example, [0 2 4 6] . This command affects the current axes. xt = xticks returns the current x-axis tick values as a vector.

What is Xlabel and Ylabel in Matlab?

xlabel( txt ) labels the x-axis of the current axes or standalone visualization. Reissuing the xlabel command replaces the old label with the new label. example. xlabel( target , txt ) adds the label to the specified target object.


2 Answers

You must use multiple axes to achieve this effect because MATLAB doesn't provide separate TickLength properties for X and Y axes.

Example:

x=linspace(0,4*pi);
y=sin(x);
ax=plotyy(x,y,0,0);
set(ax(1),'XTick',[]);
set(ax(1),'YColor',get(ax(1),'XColor'))
set(ax(2),'TickLength',[0 0]);
set(ax(2),'YTick',[]);

This is a bit hacky, but it works by using the extra y-axis provided in the plotyy() function to keep the x-axis labels with 0 tick length, while still showing the y-ticks from the original y-axis.

like image 170
Teddy Ort Avatar answered Oct 10 '22 06:10

Teddy Ort


Starting from MATLAB 2015b you can write:

ax.XAxis.TickLength = [0,0];

and diminish to zero only the X-axis tick length.

like image 43
EBH Avatar answered Oct 10 '22 06:10

EBH