Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Controlling Individual Axis Properties in a Plot

This example shows how to get properties of a ruler in MATLAB® and change ruler property values to customize an individual axis in your plot.

Numeric Ruler Objects

Create a plot and label the x-axis and y-axis.

x = linspace(0,5,1000);
y = 100*exp(x).*sin(20*x);
plot(x,y)

xlabel('x')
ylabel('y')

MATLAB creates a numeric ruler for each axis. Numeric ruler objects controls the marks, tick labels, and axis labels for a given coordinate (x, y, or z). Access the numeric ruler object associated with a particular axis through the XAxis, YAxis, or ZAxis property of the axes object. Like all graphics objects, numeric rulers have properties that you can view and modify.

ax = gca;
ax.XAxis   % the numeric ruler object for the x-axis
ans = 
  NumericRuler with properties:

             Limits: [0 5]
              Scale: 'linear'
           Exponent: 0
         TickValues: [0 0.5000 1 1.5000 2 2.5000 3 3.5000 4 4.5000 5]
    TickLabelFormat: '%g'

  Show all properties

Customize Appearance of Individual Axes Labels

Customize the look of your plot by changing the ruler properties using dot notation. For example, change the appearance of the x-axis labels by setting the FontAngle and FontSize Properties of the corresponding numeric ruler.

ax.XAxis.FontAngle = 'italic';
ax.XAxis.FontSize = 12;

Changing Exponent Value for Tick Labels

For larger values, MATLAB automatically scales the tick labels and adds an exponent to the display. Set the Exponent property of the numeric ruler associated with the y-axis to change the exponent value for the y-axis. The exponent label and the tick labels change accordingly.

ax.YAxis.Exponent = 2;

Customize Format of Tick Value Labels

Customize the format of the tick value labels for the y-axis by setting the TickLabelFormat property of the numeric ruler associated with the y-axis.

Display the tick label values along the y-axis as U.S.dollar values with two decimal places by setting the TickLabelFormat property to '$%,.2f'. The TickLabelFormat property uses the same format operators as the printf function.

profit = [20 40 50 40 50 60 70 60 70 60 60 70 80 90];
stem(profit)
xlim([0 15])

ax = gca;
ax.YAxis.TickLabelFormat = '$%,.2f';

Change other properties of the numeric ruler object to customize other aspects of an individual axis including color, width, and minor tick values.

Get All Numeric Ruler Properties

Graphics objects in MATLAB have many properties. To see all the properties of a numeric ruler, use the get command. The numeric ruler object and the axes object have several common properties. If you set the axes property, the axes passes the value to the corresponding ruler property. However, you can change ruler properties independently to customize an individual axis.

get(ax.YAxis)
                 Children: [0×0 GraphicsPlaceholder]
                    Color: [0.1500 0.1500 0.1500]
                Direction: 'normal'
                 Exponent: 0
             ExponentMode: 'auto'
                FontAngle: 'normal'
                 FontName: 'Helvetica'
                 FontSize: 10
            FontSmoothing: 'on'
               FontWeight: 'normal'
         HandleVisibility: 'off'
                    Label: [1×1 Text]
                   Limits: [0 90]
               LimitsMode: 'auto'
                LineWidth: 0.5000
                MinorTick: 'off'
          MinorTickValues: [2 4 6 8 12 14 16 18 22 24 26 28 32 34 36 38 42 44 46 48 52 54 56 58 62 64 66 68 72 74 76 78 82 84 86 88]
      MinorTickValuesMode: 'auto'
                   Parent: [1×1 Axes]
                    Scale: 'linear'
            TickDirection: 'in'
        TickDirectionMode: 'auto'
          TickLabelFormat: '$%,.2f'
     TickLabelInterpreter: 'tex'
        TickLabelRotation: 0
    TickLabelRotationMode: 'auto'
               TickLabels: {10×1 cell}
           TickLabelsMode: 'auto'
               TickLength: [0.0100 0.0250]
               TickValues: [0 10 20 30 40 50 60 70 80 90]
           TickValuesMode: 'auto'
                  Visible: 'on'
Was this topic helpful?