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(
increases the font size by a factor of 1.1."increase"
)
fontsize(
decreases the font size by a factor of 0.9."decrease"
)
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(
resets the font size and font units to the default, automatic values."default"
)
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")
Increase the font size to 16 points.
fontsize(16,"points")
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])
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 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)
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")
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 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;
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")
Input Arguments
collapse all
size
— Font size
positive scalar value
Font size, specified as a positive scalar value.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
units
— Font size units
"points"
| "pixels"
| "inches"
| "centimeters"
Font size units, specified as "points"
, "pixels"
, "inches"
, or "centimeters"
.
sfactor
— Scale 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
obj
— Object 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
R2023a: Optionally omit obj
argument
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.
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
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office