主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

fontsize

更改图窗中对象的字体大小

自 R2022a 起

    说明

    fontsize(size,units) 设置当前图窗中所有文本的字体大小和字体单位。如果图窗包含其他图形对象,如 UI 组件或具有图例的坐标区对象,fontsize 还会为图窗中的这些对象设置字体大小和字体单位。字体单位可以是 "points""pixels""inches""centimeters"

    在 R2023a 之前的版本中: 所有语法都需要使用图形对象作为第一个输入,例如 fontsize(obj,size,units)

    示例

    fontsize("increase") 将字体大小增大 1.1 倍。

    示例

    fontsize("decrease") 将字体大小缩小 0.9 倍。

    fontsize(scale=sfactor)sfactor 因子缩放字体大小。例如,使用 1.2 的缩放因子将按 120% 进行缩放。

    示例

    fontsize("default") 将字体大小和字体单位重置为默认自动值。

    示例

    fontsize(obj,___) 设置图形对象 obj 中所有文本的字体大小。如果 obj 包含其他图形对象,fontsize 还会设置这些对象的字体大小。指定 obj 作为上述任何语法中的第一个输入参量。

    示例

    示例

    全部折叠

    创建一个绘图,其中包含两个线条以及标题和图例。

    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.

    将字体大小增大到 16 磅。

    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.

    在 R2023a 之前:将 gcf 指定为 fontsize 函数的第一个参量。例如,fontsize(gcf,16,"points")

    用几个具有不同字体大小的文本元素创建一个图。

    [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.

    使用 1.2 的缩放因子将图窗中的字体大小缩放到 120%。fontsize 函数可单独缩放每个字体大小,从而保持各字体的相对大小。

    fontsize(gcf,scale=1.2)

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

    使用 tiledlayoutnexttile 函数创建一个由几个图组成的分块布局。

    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.

    放大散点图的字体大小,并将其他两个图的字体大小更改为 10 个像素。

    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.

    要撤消所有分块图的字体大小更改,请将字体大小和单位重置为其默认值。通过使用 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.

    创建以下函数文件,并将其保存为 MATLAB® 路径上的 myapplayout.m。此函数返回一个简单 App 的布局,以使用不同绘图类型绘制数据。

    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
    

    调用该函数并将返回的图窗对象赋给 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.

    使用 f 增大图窗中所有文本的字体大小,直到更易于阅读。此处,fontsize 函数按 1.1 的缩放因子单独增大每个字体大小,从而保持各字体的相对大小。

    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.

    输入参数

    全部折叠

    字体大小,指定为正标量值。

    数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    字体大小单位,指定为 "points""pixels""inches""centimeters"

    缩放因子,指定为标量。目标对象下的所有字体大小都按 sfactor 缩放。使用大于 1 的缩放因子增大字体大小,或使用小于 1 的缩放因子减小字体大小。

    示例: fontsize(gcf,scale=0.8) 将文本缩放到其原始大小的 80%。

    数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    具有文本的对象或容器,指定为图形对象或由图形对象组成的数组。fontsize 函数设置指定对象中文本的字体大小。如果 obj 包含其他图形对象,例如包含 UI 组件的图窗或具有图例的坐标区对象,则该函数还会为 obj 内的这些对象设置字体大小和字体单位。没有 FontSize 属性的对象不受影响。

    版本历史记录

    在 R2022a 中推出

    全部展开