Quantcast

Documentation Center

  • Trial Software
  • Product Updates

xlabel

Label x-axis

Syntax

Description

example

xlabel(str) labels the x-axis of the current axes with the string, str. Each axes graphics object has one predefined x-axis label. Reissuing the xlabel command causes the new label to replace the old label. Labels appear beneath the axis in a two-dimensional view and to the side or beneath the axis in a three-dimensional view.

example

xlabel(str,Name,Value) additionally specifies the text object properties using one or more Name,Value pair arguments.

example

xlabel(axes_handle,___) adds the label to the axes specified by axes_handle. This syntax allows you to specify the axes to which to add a label. axes_handle can precede any of the input argument combinations in the previous syntaxes.

example

h = xlabel(___) returns the handle to the text object used as the x-axis label. The handle is useful when making future modifications to the label.

Examples

expand all

Label x-Axis with String

figure
plot((1:10).^2)
xlabel('Population')

MATLAB® displays Population beneath the x-axis.

Label x-Axis with Numeric Input

figure
plot((1:10).^2)
xlabel(123)

MATLAB® displays 123 beneath the x-axis.

Create Multiline Label

Create a multiline label using a multiline cell array.

figure
plot((1:10).^2)
xlabel({date;'Population';'in Years'})

Include SuperScript or Subscript in Label

Use the '^' and '_' characters to include superscripts and subscripts in the axis labels. Use braces to modify more than one character.

t = linspace(0,1);
y = exp(t);
figure
plot(t,y);
xlabel('t_{seconds}')
ylabel('e^t')

Create Label and Set Font Properties

Use Name,Value pair arguments to set the font size, font weight, and text color properties of the x-axis label.

figure
plot((1:10).^2)
xlabel('Population','FontSize',12,...
       'FontWeight','bold','Color','r')

'FontSize',12 displays the label text in 12-point font. 'FontWeight','bold' makes the text bold. 'Color','r' sets the text color to red.

Label x-Axis of Specific Axes

Create a figure with two subplots and return the axes handles, s(1) and s(2).

figure
s(1) = subplot(2,1,1);
plot((1:10).^2)
s(2) = subplot(2,1,2);
plot((1:10).^3)

Label the x-axis of the top plot by referring to its axes handle, s(1).

xlabel(s(1),'Population')

Label x-Axis and Return Object Handle

Label the x-axis and return the handle to the text object used as the label.

figure
plot((1:10).^2)
str = 'Population';
h = xlabel(str);

MATLAB® returns the object handle in the output variable, h.

Set the color of the label to red, using the handle.

set(h,'Color','red')

Input Arguments

expand all

str — Text to display as x-axis labelstring

Text to display as the x-axis label, specified as a string or the name of a function that returns a string.

Example: 'myLabel'

axes_handle — Axes handlehandle

Axes handle, which is the reference to an axes object. Use the gca function to get the handle to the current axes, for example, axes_handle = gca;.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Color','red','FontSize',12 adds an x-axis label with red, 12-point font.

In addition to the following, you can specify other text object properties using Name,Value pair arguments. See Text Properties.

'Color' — Text color[0 0 0] (black) (default) | 3-element RGB vector | string

Text color, specified as the comma-separated pair consisting of 'Color' and a 3-element RGB vector or a string containing the short or long name of the color. The RGB vector is a 3-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0 1].

The following table lists the predefined colors and their RGB equivalents.

RGB Value

Short Name

Long Name

[1 1 0]

y

yellow

[1 0 1]

m

magenta

[0 1 1]

c

cyan

[1 0 0]

r

red

[0 1 0]

g

green

[0 0 1]

b

blue

[1 1 1]

w

white

[0 0 0]

k

black

Example: 'Color',[0 1 0]

Example: 'Color','green'

'FontAngle' — Character slant'normal' (default) | 'italic' | 'oblique'

Character slant, specified as the comma-separated pair consisting of 'FontAngle' and one of these values: 'normal', 'italic', or 'oblique'. MATLAB® uses the FontAngle property to select a font from those available on your particular system. Generally, setting this property to italic or oblique selects a slanted font.

Example: 'FontAngle','italic'

'FontName' — Font name'Helvetica' (default) | string | 'FixedWidth'

Font name, specified as the comma-separated pair consisting of 'FontName' and a string. The string specifies the name of the font to use for the text object. To display and print properly, this must be a font that your system supports.

To use a fixed-width font that looks good in any locale, use the case-sensitive string 'FixedWidth'. This eliminates the need to hard-code the name of a fixed-width font, which might not display text properly on systems that do not use ASCII character encoding.

Example: 'FontName','Courier'

'FontSize' — Font size10 points (default) | scalar

Font size, specified as the comma-separated pair consisting of 'FontSize' and a scalar in units determined by the FontUnits property. The default value for FontUnits is points.

Example: 'FontSize',12.5

'FontUnits' — Font size units'points' (default) | 'normalized' | 'inches' | 'centimeters' | 'pixels'

Font size units, specified as the comma-separated pair consisting of 'FontUnits' and one of the following strings:

  • 'points'

  • 'normalized'

  • 'inches'

  • 'centimeters'

  • 'pixels'

When the value of FontUnits is 'normalized', MATLAB interprets the value of FontSize as a fraction of the height of the parent axes. When you resize the axes, MATLAB modifies the screen FontSize accordingly. points, inches, centimeters, and pixels are absolute units. 1 point = 1/72 inch

    Note:   When setting both the FontSize and the FontUnits, you must set the FontUnits property first so that MATLAB can correctly interpret the specified FontSize. For example, to set the font size to 0.3 inches, call 'FontUnits','inches','FontSize',0.3 in the argument list.

'FontWeight' — Weight of text characters'normal' (default) | 'bold' | 'light' | 'demi'

Weight of text characters, specified as the comma-separated pair consisting of 'FontWeight' and one of the following strings:

  • 'normal'

  • 'bold'

  • 'light'

  • 'demi'

MATLAB uses the FontWeight property to select a font from those available on your particular system. Generally, setting this property to 'bold' or 'demi' causes MATLAB to use a bold font.

Example: 'FontWeight','bold'

'Interpreter' — Character interpretation'tex' (default) | 'latex' | 'none'

Character interpretation, specified as the comma-separated pair consisting of 'Interpreter' and one of the following strings.

Interpreter valueResult
'tex'Supports a subset of plain TeX markup language. See the String property for a list of supported TeX instructions.
'latex'Supports a basic subset of the LaTeX markup language.
'none'Interprets all characters as literal characters.

Example: 'Interpreter','latex'

Output Arguments

expand all

h — Handle to text object used as x-axis labelscalar

Handle to the text object used as the x-axis label, returned as a scalar. This is a unique identifier, which you can use to query and modify the properties of the label.

See Also

| | | |

Was this topic helpful?