Change Tick Marks and Tick Labels of Graph

This example shows how to change the tick marks and the tick mark labels. MATLAB® chooses tick mark locations based on the range of the data, and automatically uses numeric labels at each tick mark.

Create Simple Line Chart

Define x as 200 linearly spaced values between -10 and 10. Define y as the cosine of x. Plot the data.

x = linspace(-10,10,200);
y = cos(x);
plot(x,y)

Change Tick Marks

Change the location of the tick marks on the plot by setting the XTick and YTick properties of the axes. Use gca to refer to the current axes. Define the tick marks as a vector of increasing values. The values do not need to be equally spaced. Starting in R2014b, use dot notation to set properties. If you are using an earlier release, use the set function instead.

ax = gca;
ax.XTick = [-3*pi,-2*pi,-pi,0,pi,2*pi,3*pi];
ax.YTick = [-1,-0.5,0,0.5,1];

Change Tick Mark Labels

Specify tick mark labels by setting the XTickLabel and YTickLabel properties of the axes. Set these properties using a cell array of strings with the desired labels. If you do not specify enough text labels for all the tick marks, then MATLAB cycles through the labels.

ax.XTickLabel = {'-3\pi','-2\pi','-\pi','0','\pi','2\pi','3\pi'};
ax.YTickLabel = {'min = -1','-0.5','0','0.5','max = 1'};

See Also

| |

Related Examples

Was this topic helpful?