Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Adding Axis Labels to Graphs

### What Are Axis Labels?

In a MATLAB® figure, an axis label is a text string aligned with the x-, y-, or z-axis in a graph. Axis labels can help explain the meaning of the units that each axis represents. The following figure shows axis labels for both axes, created at the Command Line using the xlabel and ylabel functions, as follows:

xlabel('Time (Years)')
ylabel('Population Size')


This is the simplest way to add axis labels using MATLAB code, and is described in more detail in Using Axis-Label Commands . You can add axis labels to a graph in other ways, as described in the following sections.

 Note   Although you can use free-form text annotations to create axes labels, it is not recommended. Axis labels are anchored to the axes they describe; text annotations are not. If you move or resize your axes, the labels automatically move with the axes. Additionally, if you cut a label and then paste it back into a figure, the label is no longer anchored to the axes.

### Using the Label Options on the Insert Menu

1. Click the Insert menu and choose the label option that corresponds to the axis you want to label: X Label, Y Label, or Z Label. A text entry box opens along the axis or around an existing axis label.

 Note   Text editing boxes for the y- and z-axis labels are horizontal; the text you enter is automatically rotated to align the label with the axis when you finish entering text.
2. Enter the text of the label, or edit the text of an existing label.

3. Click anywhere else in the figure background to close the text entry box around the label. If you click on another object in the figure, such as an axes or line, you close the label text entry box but also automatically select the object you clicked.

 Note   After you use the Insert menu to add an axis label, plot edit mode is enabled in the figure, if it was not already enabled. You can modify axis labels in plot edit mode by double-clicking them and typing new text.

### Using the Property Editor to Add Axis Labels

To add labels to a graph using the Property Editor,

1. Start plot editing mode by selecting Edit Plot from the figure Tools menu.

2. Start the Property Editor by double-clicking on the axes in the graph. You can also start the Property Editor by right-clicking on the axes and selecting Properties from the context menu or by selecting Property Editor from the View menu.

The Property Editor displays the set of property panels specific to axes objects.

3. Select the X Axis, Y Axis, or Z Axis tab, depending on which axis label you want to add. Enter the label text in the text entry box.

#### Rotating Axis Labels

You can rotate axis labels using the Property Editor:

1. Start plot editing mode by selecting Edit Plot from the figure Tools menu.

2. Display the Property Editor by selecting (left-clicking) the axis label you want to rotate. Right-click over the selected text, then choose Properties from the context menu.

3. Click the More Properties button to display the Property Inspector.

4. Select the Rotation property text field. A value of 0 degrees orients the label in the horizontal position.

5. With the left mouse button down on the selected label, drag the text to the desired location and release.

### Using Axis-Label Commands

You can add x-, y-, and z-axis labels using the xlabel, ylabel, and zlabel functions. For example, these statements label the axes and add a title.

xlabel('t = 0 to 2\pi','FontSize',16)
ylabel('sin(t)','FontSize',16)
title('\it{Value of the Sine from Zero to Two Pi}','FontSize',16)

The labeling commands automatically position the text string appropriately. MATLAB interprets the characters immediately following the backslash (\) as TeX commands. These commands draw symbols such as Greek letters and arrows.

See the text String property for a list of TeX character sequences. See also the texlabel function for converting MATLAB expressions to TeX symbols.

#### Rotating Axis Labels Using Commands

Axis labels are text objects that you can rotate by specifying a value for the object's Rotation property. The handles of the x-, y-, and z-axis labels are stored in the axes XLabel, YLabel, and ZLabel properties respectively.

Therefore, to rotate the y-axis label so that the text is horizontal:

1. Get the handle of the text object using the axes YLabel property.

2. Set the Rotation property to 0.0 degrees.

For example, this statement rotates the text of the y-axis label on the current axes:

set(get(gca,'YLabel'),'Rotation',0.0)

#### Repositioning Axis Labels

You can reposition an axis label by dragging the text.

1. Start plot editing mode by selecting Edit Plot from the figure Tools menu.

2. Select the text of the label you want to reposition (handles appear around the text object).

3. With the left mouse button down on the selected label, drag the text to the desired location and release.