Main Content

fontsize

Change font size for objects in a figure

Since R2022a

    Description

    fontsize(size,units) sets the font size and font units for all of the text within the current figure. If the figure contains other graphics objects, such as UI components or an axes object with a legend, fontsize also sets the font size and font units for those objects in the figure. Font units can be "points", "pixels", "inches", or "centimeters".

    Before R2023a: All syntaxes require a graphics object as the first input, such as fontsize(obj,size,units).

    example

    fontsize("increase") increases the font size by a factor of 1.1.

    example

    fontsize("decrease") decreases the font size by a factor of 0.9.

    fontsize(scale=sfactor) scales the font size by a factor of sfactor. For example, use a scale factor of 1.2 to scale by 120%.

    example

    fontsize("default") resets the font size and font units to the default, automatic values.

    example

    fontsize(obj,___) sets the font size for all of the text within graphics object obj. If obj contains other graphics objects, fontsize also sets the font size for those objects. Specify obj as the first input argument in any of the previous syntaxes.

    example

    Examples

    collapse all

    Create a plot containing two lines with a title and a legend.

    plot([0 1; 1 2])
    title("Two Very Straight Lines")
    legend("Blue Line","Red Line")

    Figure contains an axes object. The axes object with title Two Very Straight Lines contains 2 objects of type line. These objects represent Blue Line, Red Line.

    Increase the font size to 16 points.

    fontsize(16,"points")

    Figure contains an axes object. The axes object with title Two Very Straight Lines contains 2 objects of type line. These objects represent Blue Line, Red Line.

    Before R2023a: Specify gcf as the first argument to the fontsize function. For example, fontsize(gcf,16,"points").

    Create a plot with several text elements of varying font sizes.

    [X,Y,Z] = peaks;
    contourf(X,Y,Z,LineColor="#4F4F4F")
    title("Peak Elevation")
    colorbar
    annotation("textarrow",[.53 .41],[.65 .47],String="Local maxima")
    annotation("textarrow",[.53 .59],[.65 .55])

    Figure contains an axes object. The axes object with title Peak Elevation contains an object of type contour.

    Scale the font sizes in the figure by 120% by using a scale factor of 1.2. The fontsize function scales each font size individually, maintaining the relative sizes of the fonts.

    fontsize(gcf,scale=1.2)

    Figure contains an axes object. The axes object with title Peak Elevation contains an object of type contour.

    Create a tiling of several plots by using the tiledlayout and nexttile functions.

    x = linspace(0,3*pi,200);
    y = cos(x) + rand(1,200);
    t = tiledlayout(2,2);
    
    % Top scatter plot in tiles 1,2
    ax1 = nexttile([1 2]);
    scatter(x,y)
    title("Random Variance on Cosine")
    
    % Lower polar plot in tile 3
    ax2 = nexttile;
    plot(x,cos(x)+0.5)
    
    % Lower histogram in tile 4
    ax3 = nexttile;
    histogram(y,20)

    Figure contains 3 axes objects. Axes object 1 with title Random Variance on Cosine contains an object of type scatter. Axes object 2 contains an object of type line. Axes object 3 contains an object of type histogram.

    Scale up the font size of the scatter plot, and change the font size of the other two plots to 10 pixels.

    fontsize(ax1,scale=1.2)
    fontsize([ax2 ax3],10,"pixels")

    Figure contains 3 axes objects. Axes object 1 with title Random Variance on Cosine contains an object of type scatter. Axes object 2 contains an object of type line. Axes object 3 contains an object of type histogram.

    To undo the font size changes across all the tiled plots, reset the font sizes and units to their default values. Apply this change to all three plots by using the current figure object returned by gcf.

    fontsize(gcf,"default")

    Figure contains 3 axes objects. Axes object 1 with title Random Variance on Cosine contains an object of type scatter. Axes object 2 contains an object of type line. Axes object 3 contains an object of type histogram.

    Create the following function file, and save it as myapplayout.m on your MATLAB® path. This function returns the layout for a simple app to plot data using different plot types.

    function fig = myapplayout
    % Create figure window
    fig = uifigure;
    
    % Create UI components
    ax = uiaxes(fig,Position=[15 70 535 340]);
    lbl = uilabel(fig,Position=[30 15 100 35],Text="Choose Plot Type:");
    b1 = uibutton(fig,Position=[140 15 180 35],Text="Surf");
    b2 = uibutton(fig,Position=[350 15 180 35],Text="Mesh");
    
    % Configure UI component appearance
    surf(ax,peaks);
    fontsize(fig,8,"pixels")
    title(ax,"Peak Surface",FontSize=11)
    end
    

    Call the function and assign the returned figure object to f.

    f = myapplayout;

    Figure contains an axes object and other objects of type uilabel, uibutton. The axes object with title Peak Surface contains an object of type surface.

    Use f to increase the font size of all text in the figure until it is more easily readable. Here, the fontsize function increases each font size individually by a scale factor of 1.1, maintaining the relative sizes of the fonts.

    fontsize(f,"increase")
    fontsize(f,"increase")
    fontsize(f,"increase")

    Figure contains an axes object and other objects of type uilabel, uibutton. The axes object with title Peak Surface contains an object of type surface.

    Input Arguments

    collapse all

    Font size, specified as a positive scalar value.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Font size units, specified as "points", "pixels", "inches", or "centimeters".

    Scale factor, specified as a scalar. All font sizes under the target object are scaled by sfactor. Use a scale factor greater than 1 to increase the font sizes or a factor less than 1 to decrease the font sizes.

    Example: fontsize(gcf,scale=0.8) scales the text to 80% of its original size.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Object or container with text, specified as a graphics object or array of graphics objects. The fontsize function sets the font size of text in the specified objects. If obj contains other graphics objects, such as a figure that contains UI components or an axes object that has a legend, the function also sets the font size and font units for those objects within obj. Objects without a FontSize property are not affected.

    Version History

    Introduced in R2022a

    expand all