MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

The annotations of a MuPAD® plot may include a legend. A legend is a small table that relates the color of an object with some text explaining the object:

f := 3*x*sin(2*x):
g := 4*x^2*cos(x):
h := sin(4*x)/x:
plotfunc2d(f, g, h, x = 0..PI/2):

By default, legends are provided only by plotfunc2d and plotfunc3d. These routines define the legend texts as the expressions with which the functions are passed to plotfunc2d or plotfunc3d, respectively. A corresponding plot command using primitives of the plot library does not generate the legend automatically:

plot(plot::Function2d(f, x = 0..PI/2, Color = RGB::Red),
     plot::Function2d(g, x = 0..PI/2, Color = RGB::Green),
     plot::Function2d(h, x = 0..PI/2, Color = RGB::Blue)):

However, legends can be requested explicitly:

plot(plot::Function2d(f, x = 0..PI/2, Color = RGB::Red,
                      Legend = "Function 1: ".expr2text(f)),
     plot::Function2d(g, x = 0..PI/2, Color = RGB::Green,
                      Legend = "Function 2: ".expr2text(g)),
     plot::Function2d(h, x = 0..PI/2, Color = RGB::Blue,
                      Legend = "Function 3: ".expr2text(h))

Each graphical primitive accepts the attribute Legend. Passing this attribute to an object triggers several actions:

  • The object attribute LegendText is set to the given string.

  • The object attribute LegendEntry is set to TRUE.

  • A hint is sent to the scene containing the object advising it to use the scene attribute LegendVisible = TRUE.

The attributes LegendText and LegendEntry are visible in the “object inspector” of the interactive viewer (see section Viewer, Browser, and Inspector: Interactive Manipulation) and can be manipulated interactively for each single primitive after selection in the “object browser.” The attribute LegendVisible is associated with the scene object accessible via the “object browser.”

At most 20 entries can be displayed in a legend. If more entries are specified in a plot command, the surplus entries are ignored. Further, the legend may not cover more than 50% of the height of the drawing area of a scene. Only those legend entries fitting into this space are displayed; remaining entries are ignored.

If the attribute LegendText = TRUE is set for a primitive, its legend entry is determined as follows:

  • If the attribute LegendText is specified, its value is used for the legend text.

  • If no LegendText is specified, but the Name attribute is set, the name is used for the legend text.

  • If no Name attribute is specified either, the type of the object such as Function2d, Curve2d etc. is used for the legend text.

Here are all attributes relevant for legends:

Attributes for Legend

attribute name possible values/example meaning default browser entry
Legendstring sets LegendText to the given string, LegendEntry to TRUE, and LegendVisible to TRUE.   
LegendEntryTRUE, FALSEadd this object to the legend? TRUE for function graphs, curves, and surfaces, FALSE otherwise primitive
LegendTextstring legend text  primitive
LegendVisibleTRUE, FALSElegend on/off TRUE for plotfunc2d/3d, FALSE otherwise Scene2d/3d
LegendPlacementTop, Bottomvertical placement BottomScene2d/3d
LegendAlignmentLeft, Center, Righthorizontal alignment CenterScene2d/3d
LegendFontsee section Fontsfont for the legend text sans-serif 8 Scene2d/3d