# 2.10 How to Set the Tick Locations and Labels

Many users call asking for examples of how to specify the location and name of their tick marks. For example, if you are plotting earnings versus time, you may want to label your X-tick marks with weeks, months, or even years. Below is a list of properties that control the X-tick locations and labels. These are properties of axes.

    XLim
    XLimMode               [{auto}|manual]
    XTick
    XTickMode              [{auto}|manual]
    XTickLabels

NOTE: The Y and Z axes have similar properties that start with Y or Z respectively.

The XLim property is used to store the limits of the X-axis. By default, MATLAB chooses the limits; however, the user can specify the limits using axis([xmin xmax ymin ymax zmin zmax]) or set(gca,XLim,[xmin xmax]). Setting this property will automatically change the XLimMode to manual.

XLimMode is used to determine whether MATLAB or the user is controlling the X-limits. By default, it is set to auto, which implies that MATLAB chooses the limits. To prevent MATLAB from changing the limits when the figure is resized or printed, set this property to manual. If you set the XLim property, or use the axis command, XLimMode is automatically set to manual.

XTick is the property in which MATLAB stores the location of the X-tick marks. Generally, this property is used by MATLAB; however, the user can set this property so that only the desired tick marks are drawn. Setting this property automatically changes the XTickMode property to manual.

XTickMode is used to determine whether MATLAB or the user controls the tick locations. By default, it is set to auto, which implies that MATLAB controls the locations of the tick marks. To prevent MATLAB from changing the tick locations or number of ticks when the figure is resized or printed, change this property to manual. If XTick is set by the user, this property is automatically set to manual.

XTickLabels is the property in which MATLAB stores the strings used to label the tick marks. Normally, this property contains the string representation of the XTick property. For example, if XTick contains the vector [2 4 6 8], then XTickLabels contains the following string array:

  2
  4
  6
  8

Usually, the number of rows in XTickLabels is equal to the number of tick marks. If this is not true, then MATLAB will cycle through the X-tick labels to label each of the tick marks. For example, if the previous string array only contained the first two rows, the ticks along the X-axis would be labelled 2-4-2-4.

An important attribute of the XTickLabels is that it is a string array. This means that every row must contain the same number of columns. So, if you change the XTickLabels to contain 1 and 100, you will have to pad the 1 with spaces. For example:

    set(gca,'XTickLabels',['1  ';'100'])

------------------------------------------------------------

Now that you understand the properties required to change the tick location and labels, let's work through an example:

    % Generate a random vector of earning that can range from
    % $0 - $2000.  Each element of earnings correspond to the
    % earnings for a given month.
    earnings = 1000 + 1000*rand(1,12) - 1000*rand(1,12);
    % Generate a bar plot of the earnings with 12 bins
    % (1 bin per month)
    bar(earnings);
    % Set the XLim so that it ranges from .5 - 12.5.
    % The reason that it starts at .5 and ends at 12.5 is
    % because each bin is .5 data units wide.
    set(gca,'XLim',[.5 12.5]);% This automatically sets the
                             % XLimMode to manual.
    % Set XTick so that only the integer values that
    % range from 0.5 - 12.5 are used.
    set(gca,'XTick',[1:12])  % This automatically sets
                             % the XTickMode to manual.
    % Set the XTickLabels so that abbreviations for the
    % months are used.
    months = ['Jan';
              'Feb';
              'Mar';
              'Apr';
              'May';
              'Jun';
              'Jul';
              'Aug';
              'Sep';
              'Oct';
              'Nov';
              'Dec'];
    set(gca,'XTickLabels',months)

-------------------------------------------------------------

Remember, the Y- and Z-axes both have properties similar to the ones described in this Technical Note.

(c) Copyright 1994 by The MathWorks, Inc.