legend
legend()orlegend 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.
| Code | Input | Output | Description |
legend() or legend off | no input | no output | legend("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)")
· plot: figure 3 (50 points) · plot: figure 4 (50 points)
| Code | Input | Output | Description |
legend() or legend off | no input | no output | legend 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
· plot: figure 5 (50 points)
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.