2.6 Text, Labels, and Title Font Control

This technical note provides examples of how to control the fonts used by text, axis labels and titles, and tick marks.

Below is a list of properties that both axes and text share:

Property Name             Property Value
FontAngle                 normal
FontName                  Helvetica
FontSize                  12
FontWeight                normal
FontAngle refers to the angle of the font. By default, this is normal; however, the possible settings for this property are:

Settings                  Description
normal                    Normal font
italic                    Italic font
oblique                   Oblique font
Example:

h = text(0.5,0.5, 'This is italics text');
set(h, 'FontAngle', 'italic')
set(gca,'FontAngle','oblique')
FontName refers to the name of the font. Currently, MATLAB supports the following eleven font families:

Avante Garde              Palatino
Bookman                   Symbol
Courier                   Times
Helvetica                 Zapf Chancery
Helvetica Narrow          Zapf Dingbats
New Century Schoolbook
The default font is Helvetica.

Example:

h = text(0.5,0.5, 'This is Courier font text');
set(h, 'FontName', 'Courier')
set(gca,'FontName','Bookman')
FontSize is the point size of the font. By default, this is set to 12; however, you can set this to any font size, assuming your machine has the font installed. (Note: one point = 1/72 inch)

Example:

h = text(0.5,0.5, 'This is 16-point text');
set(h, 'FontSize', 16)
set(gca,'FontSize',10)
FontWeight refers to the weight of the font. By default, this is set to normal. The other possible settings are:

Weight                    Description
bold                      Bold weight font
demi                      Medium bold weight font
light                     Fine weight font
normal                    Normal weight font
Example:

h =  text(0.5,0.5, 'This is bold text');
set(h, 'FontWeight', 'bold') 
set(gca,'FontWeight','light')
These settings can be combined to create almost any combination that your machine supports. For example, to specify a 10-point Helvetica-Bold Oblique font, do one of the following:

h = text(X,Y,'Text String','FontName','Helvetica', ...
    'FontSize',10,'FontWeight','bold','FontAngle', ...
    'oblique');
h = text(X,Y,'Text String');
set(h,'FontName','Helvetica','FontSize',10, ...
    'FontWeight','bold','FontAngle','oblique');
where X and Y are the x and y coordinates.

If your machine does not support a particular font, then MATLAB uses the following rules for selecting the current font:

Titles and Labels

To change the font characteristics of titles and labels, you must first get the handle of the existing title or label. To change the FontName of a title to Helvetica, first display the title, get the handle to the title, and then use the set command to set the FontName to Helvetica:

Example:

title('This is the title')
h = get(gca, 'title');
set(h, 'FontName', 'Helvetica')
Use the same procedure to set the FontName of labels (xlabels, ylabels, and zlabels).

Example:

xlabel('This is the label')
h = get(gca, 'xlabel');
set(h, 'FontName', 'Times')

Default Property Values

Changing the font characteristics of a text object and having the change recognized throughout the MATLAB session, in a figure only, or in the axes only is known as setting the default property value of an object (in this case text). There are three different levels at which default values can be set. You can set the default value for the:

You can identify default values with a string beginning with the word Default followed by the object type and finally, the object property name. Below is the basic structure for setting defaults:

set(<ancestor>, 'Default<object type><property name>', ...
<property value>)
For example, the following sets the default FontName used by text to Symbol for the current axes only:

set(gca, 'DefaultTextFontName', 'symbol')
To specify the default FontName for the current figure, use gcf as the handle. To specify the default for the entire session, use 0.

Setting the default FontName of text will affect all text objects. Setting the default FontName will set the font used for axis labels, titles, and tick mark labels.

(c) Copyright 1994 by The MathWorks, Inc.