Main Content

subtitle

Add subtitle to plot

    Description

    example

    subtitle(txt)adds the specified subtitle text to the current axes.

    example

    subtitle(___,Name,Value)sets properties on the text object using one or more name-value pair arguments. Specify the properties after all other input arguments. For a list of properties, seeText Properties.

    example

    subtitle(target,___)specifies the target object for the subtitle. The target object can be any type of axes, a tiled chart layout, or an array of objects. Specify the target object before all other input arguments.

    example

    t = subtitle(___)returns the text object for the subtitle. Usetto set properties on the object after creating the subtitle. For a list of properties, seeText Properties.

    Examples

    collapse all

    Create a plot. Add a title with thetitle函数。然后add a subtitle with thesubtitle函数。

    plot([0 2],[1 5]) title('Straight Line') subtitle('Slope = 2, y-Intercept = 1')

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Create a plot, and add a title to the plot. Defineslopevalueandyinterceptas numeric variables. Definetxtas a combination of literal text and the values ofslopevalueandyinterceptconverted to character vectors. Then, passtxtto thesubtitlefunction to display the subtitle.

    plot([0 2],[1 5]) title('Straight Line') slopevalue = 4; yintercept = 1; txt = ['Slope = 'int2str(slopevalue)', y-Intercept = 'int2str(yintercept)]; subtitle(txt)

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Create a plot. Add a title with thetitle函数。然后, call thesubtitlefunction, and specify the color using the'Color'name-value pair argument. The color can be a color name, such as'red', or you can specify a custom color using an RGB triplet or hexadecimal color code. In this case, specify'red'.

    plot([0 2],[1 5]) title('Straight Line') subtitle('Slope = 2, y-Intercept = 1','Color','red')

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Alternatively, call thesubtitlefunction with an output argument to return the text object. Then set the color on the text object. In this case, specify the hexadecimal color code '#DD5500'.

    txt = subtitle('Plot of y = 2x + 1'); txt.Color ='#DD5500';

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Create a plot, and add a title with thetitle函数。Create a character vector containing TeX markup with custom colors for different words in the subtitle. Then pass the character vector to thesubtitle函数。

    plot([0 2],[1 5]) title('Straight Line') txt = ['An {\color{magenta}Attractive '...'\color[rgb]{0 .5 .5}and \color{red}Colorful} Subtitle']; subtitle(txt)

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Create a histogram, and add a title with thetitle函数。Create a character vector containing TeX markup with Greek symbols. Then pass the character vector to thesubtitle函数。

    histogram(5*randn(1,50)+10) title('Population Data') txt ='{\it\mu} = 10, {\it\sigma} = 5'; subtitle(txt)

    Figure contains an axes. The axes with title Population Data contains an object of type histogram.

    Create a histogram, and add a title with thetitle函数。Create a character vector containing TeX markup that displays subscripts and superscripts. Then pass the character vector to thesubtitle函数。

    x = -10:0.1:10; y1 = x.^2; y2 = 2*x.^2; plot(x,y1,x,y2); title('Exponential Functions') txt ='y_1 = x^2 and y_2 = 2x^{2 + k}'; subtitle(txt)

    Figure contains an axes. The axes with title Exponential Functions contains 2 objects of type line.

    To display an italic font for the variables, add the\itmodifier.

    txt ='{\ity}_1 = {\itx}^2 and {\ity}_2 = 2{\itx}^{2 + \itk}'; subtitle(txt)

    Figure contains an axes. The axes with title Exponential Functions contains 2 objects of type line.

    Create a plot, and add a title with thetitle函数。然后create a subtitle containing two lines of text by passing a cell array of character vectors to thesubtitle函数。Each element in the array is a separate line of text.

    plot([0 2],[1 5]) title('Straight Line') txt = {'Slope = 2','y-Intercept = 1'}; subtitle(txt)

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Create a plot with a title. Then create a subtitle containing an underscore character that the TeX interpreter normally uses for subscripts. Set theInterpreterto'none'when you call thesubtitlefunction, so that the underscore character appears in the subtitle.

    plot([0 2],[1 5]) title('Straight Line') subtitle('y_1 = 2x + 1','Interpreter','none')

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Create a plot and add a title and a subtitle. Get the current axes, and align the title and subtitle to the left edge of the plot box by setting theTitleHorizontalAlignment属性the axes to'left'.

    plot([0 2],[1 5]) title('Straight Line') subtitle('Slope = 2, y-Intercept = 1') ax = gca; ax.TitleHorizontalAlignment ='left';

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Center the title and subtitle by setting theTitleHorizontalAlignment属性the axes to 'center'.

    ax.TitleHorizontalAlignment ='center';

    Figure contains an axes. The axes with title Straight Line contains an object of type line.

    Create two plots in a tiled chart layout. Then add a title and subtitle to each plot.

    t = tiledlayout(1,2);% Left plotax1 = nexttile; plot([0 2],[1 5]) title(ax1,'A Straight Line') subtitle(ax1,'Slope = 2, y-Intercept = 1')% Right plotax2 = nexttile; plot([0 2],[2 8]) title(ax2,'Another Straight Line') subtitle(ax2,'Slope = 3, y-Intercept = 2')

    Figure contains 2 axes. Axes 1 with title A Straight Line contains an object of type line. Axes 2 with title Another Straight Line contains an object of type line.

    输入参数

    collapse all

    Subtitle text, specified as a character vector, cell array of character vectors, or a string array. To create multiple lines of text, specify a cell array of character vectors or a string array.

    Example:subtitle('Single Line Subtitle')

    Example:subtitle(["Subtitle With" "Multiple Lines"])

    Target for the subtitle, specified as one of the following:

    • Any type of axes: anAxes,PolarAxes, orGeographicAxesobject.

    • ATiledChartLayoutobject.

    • An array of graphics objects from the preceding list. The objects must belong to the same class. To determine the class, use theclass函数。

    If you do not specify the target for the subtitle, then thesubtitlefunction adds the subtitle to the graphics object returned by thegcacommand.

    Name-Value Pair Arguments

    Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

    Example:subtitle('My Subtitle','FontSize',12)specifies a 12-point font size.

    Note

    The properties listed here are only a subset. For a complete list, seeText Properties.

    Font size, specified as a scalar value greater than0in point units. One point equals 1/72 inch. To change the font units, use theFontUnitsproperty.

    If you add a title or subtitle to an axes object, then the font size property for the axes also affects the font size for the title and subtitle. The title and subtitle font sizes are the axes font size multiplied by a scale factor. TheFontSizeproperty of the axes contains the axes font size. TheTitleFontSizeMultiplierproperty of the axes contains the scale factor. By default, the axes font size is 10 points and the scale factor is 1.1, so the title and subtitle each have a font size of 11 points.

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

    Thickness of the text characters, specified as one of these values:

    • 'normal'— Normal weight as defined by the particular font

    • 'bold'— Thicker characters outlines than normal

    MATLAB®uses theFontWeightproperty to select a font from those available on your system. Not all fonts have a bold font weight. Therefore, specifying a bold font weight could still result in the normal font weight.

    TheSubtitleFontWeightproperty for the associated axes affects theFontWeightvalue for the subtitle.

    See Also

    Functions

    Properties

    Introduced in R2020b