Qu

legend

Builtin index · Plotting

legend() or legend off

Input: no input · Output: no output

What it is

Shows or hides the key for the current panel. With no argument it turns on and places itself where it overlaps least; legend outside (bare command) reserves margin instead. See also the function form legend("label1", ...) in the Figure & Axes Control table above, which labels series rather than toggling visibility.

Overloads

legend is two unrelated builtins sharing one name: the bare command form controls the legend box itself (on/off/position), and the function-call form instead labels the panel's own series. Neither one does the other's job.

2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
legend() or legend offno inputno outputlegend("sin(x)", "cos(x)") labels the trailing two series of the current panel, in call order, and turns the legend on. Look for a legend box carrying those two exact strings.

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

t = linspace(0, 1, 50)
plot(t, sin(t))
plot(t, cos(t))
legend("sin(x)", "cos(x)")
output
· plot: figure 3 (50 points)
· plot: figure 4 (50 points)
10−279 10−248 10−217 10−186 10−155 10−124 10−93 10−62 10−31 100 -100 -50 0 50 100 (0, 1.9) 6 dB = 1 bit sin(x) cos(x)
CodeInputOutputDescription
legend() or legend offno inputno outputlegend top left is unrelated to labeling — it only moves (or, with off, hides) whatever legend already exists, using whatever labels plot's own label= set. Look for the box relocating to the top-left corner.

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

t = linspace(0, 1, 50)
plot(t, sin(t), label = "sin(x)")
legend top left
output
· plot: figure 5 (50 points)
10−279 10−248 10−217 10−186 10−155 10−124 10−93 10−62 10−31 100 -100 -50 0 50 100 (0, 1.9) 6 dB = 1 bit sin(x) cos(x) sin(x)

How it works

legend("sin(x)", "cos(x)") labels the trailing N series of the current panel, in call order, and turns the legend on; position and on/off are instead set by the bare legend ... command.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.