Change font size for objects in a figure (2024)

Change font size for objects in a figure

Since R2022a

collapse all in page

    Syntax

    fontsize(size,units)

    fontsize("increase")

    fontsize("decrease")

    fontsize(scale=sfactor)

    fontsize("default")

    fontsize(obj,___)

    Description

    example

    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.

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

    example

    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.

    Examples

    collapse all

    Change Font Size in Plot

    Open Live Script

    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")

    Change font size for objects in a figure (1)

    Increase the font size to 16 points.

    fontsize(16,"points")

    Change font size for objects in a figure (2)

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

    Scale Font Size of Text in Figure

    Open Live Script

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

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

    Change font size for objects in a figure (3)

    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)

    Change font size for objects in a figure (4)

    Change Font Size in Tiled Chart Layout

    Open Live Script

    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,2ax1 = nexttile([1 2]);scatter(x,y)title("Random Variance on Cosine")% Lower polar plot in tile 3ax2 = nexttile;plot(x,cos(x)+0.5)% Lower histogram in tile 4ax3 = nexttile;histogram(y,20)

    Change font size for objects in a figure (5)

    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")

    Change font size for objects in a figure (6)

    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")

    Change font size for objects in a figure (7)

    Change Font Size of App Text

    Open Live Script

    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 windowfig = uifigure;% Create UI componentsax = 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 appearancesurf(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;

    Change font size for objects in a figure (8)

    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")

    Change font size for objects in a figure (9)

    Input Arguments

    collapse all

    sizeFont size
    positive scalar value

    Font size, specified as a positive scalar value.

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

    unitsFont size units
    "points" | "pixels" | "inches" | "centimeters"

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

    sfactorScale factor
    scalar

    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

    objObject or container with text
    graphics object | array of graphics objects

    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

    You can optionally omit the obj argument when changing the font size of the current figure and its descendants.

    See Also

    fontname | uisetfont | axes

    Topics

    • Graphics Object Hierarchy
    • Add Title and Axis Labels to Chart
    • Add Text to Chart

    MATLAB Command

    You clicked a link that corresponds to this MATLAB command:

     

    Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

    Change font size for objects in a figure (10)

    Select a Web Site

    Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

    You can also select a web site from the following list:

    Americas

    • América Latina (Español)
    • Canada (English)
    • United States (English)

    Europe

    • Belgium (English)
    • Denmark (English)
    • Deutschland (Deutsch)
    • España (Español)
    • Finland (English)
    • France (Français)
    • Ireland (English)
    • Italia (Italiano)
    • Luxembourg (English)
    • Netherlands (English)
    • Norway (English)
    • Österreich (Deutsch)
    • Portugal (English)
    • Sweden (English)
    • Switzerland
      • Deutsch
      • English
      • Français
    • United Kingdom (English)

    Asia Pacific

    Contact your local office

    Change font size for objects in a figure (2024)
    Top Articles
    Latest Posts
    Article information

    Author: Rev. Leonie Wyman

    Last Updated:

    Views: 6180

    Rating: 4.9 / 5 (59 voted)

    Reviews: 82% of readers found this page helpful

    Author information

    Name: Rev. Leonie Wyman

    Birthday: 1993-07-01

    Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

    Phone: +22014484519944

    Job: Banking Officer

    Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

    Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.