legend — Show and configure plot legends in MATLAB and RunMat.
legend creates or updates the legend for the current axes (or an explicit axes handle). Returned handle behavior, display-name sourcing, and style controls follow MATLAB semantics.
Syntax
legend()
legend(mode)
legend(labels..., Name, Value, ...)
h = legend()
h = legend(ax)
h = legend(mode)All supported legend forms
legend()
legend(mode)
legend(labels..., Name, Value, ...)
h = legend()
h = legend(ax)
h = legend(mode)
h = legend(ax, mode)
h = legend(labels..., Name, Value, ...)
h = legend(ax, labels..., Name, Value, ...)Inputs
mode- Type
StringScalar- Required
- Yes
- Description
- Legend mode token: 'on'|'off'|'show'|'hide'|'boxon'|'boxoff'.
labels_or_pairs- Type
Any- Required
- Variadic
- Description
- Labels (string/cell/string-array) with optional trailing Name/Value legend style pairs.
ax- Type
AxesHandle- Required
- Yes
- Description
- Target axes handle.
Returns
h- Type
NumericScalar- Description
- Legend object handle.
Errors
RunMat:legend:InvalidArgument- When
- Legend arguments are malformed, unsupported, or have invalid labels/properties.
- Message
legend: invalid argument
RunMat:legend:InvalidAxes- When
- Resolved axes target is invalid or out of range.
- Message
legend: invalid axes target
RunMat:legend:Internal- When
- Internal legend state operation fails.
- Message
legend: internal operation failed
Behavior
Calling legend with no label arguments uses plotted object display names when they are available.
Labels can be passed as separate strings, string arrays, or cell arrays, following common MATLAB legend workflows.
Legend visibility and style are axes-local, so enabling or hiding a legend in one subplot does not mutate every axes in the figure.
The returned handle can be used with get and set to inspect or update location, orientation, box visibility, font styling, and related legend properties.
For a subplot-local legend, call legend on that axes. A legend snapshot does not automatically gain entries for series added later; call legend again after adding them.
Placement and effects
Legend commands operate on host graphics state. They do not run a numeric accelerator kernel or gather resident labels or style values.
A numeric graphics handle identifies local figure state, not a transferable graphics object. Invalid axes, labels, and style properties are reported by the graphics parser or state owner.
Examples
Build a legend from display names
x = linspace(0, 2*pi, 200);
h1 = plot(x, sin(x));
set(h1, 'DisplayName', 'sin(x)');
hold on;
h2 = plot(x, cos(x));
set(h2, 'DisplayName', 'cos(x)');
legend;Pass labels explicitly and position the legend
plot(1:5, [1 2 3 4 5]);
hold on;
plot(1:5, [5 4 3 2 1]);
lg = legend('rising', 'falling', 'Location', 'southwest');
get(lg, 'Type')Expected output:
ans =
'legend'Keep legends subplot-local
subplot(1, 2, 1);
plot(1:5, 1:5);
lg = legend('left');
subplot(1, 2, 2);
plot(1:5, [5 4 3 2 1]);Using legend with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how legend changes the result.
Run a small legend example, explain the result, then change one input and compare the output.
FAQ
How do I move the legend to a specific corner?⌄
Pass the 'Location' property when creating the legend, or update it afterward with set. Standard MATLAB location strings work: 'northeast', 'southwest', 'northwest', 'southeast', etc.
lg = legend('a', 'b');
set(lg, 'Location', 'northwest');Can I show only some series in the legend?⌄
Set 'DisplayName' only on the series you want labeled, then call legend with no arguments. Series without a display name are excluded. Alternatively, pass explicit label strings to legend — only the first N plotted objects get entries.
Do I need to call legend again after adding more series with hold on?⌄
Yes. The legend snapshot is taken when legend is called, so new series added after the initial legend call won't appear until you call legend again. Set display names on all series first, then call legend once at the end.
Related Plotting functions
2D Charts
area · bar · errorbar · heatmap · hist · histogram · loglog · pie · plot · scatter · semilogx · semilogy · stairs · stem
Other
addpoints · ancestor · animatedline · autumn · axes · barh · bone · caxis · cla · clf · clim · colorcube · colororder · cool · copper · copyobj · cylinder · daspect · datacursormode · dataTipTextRow · drawnow · fcontour · figure · fill · fill3 · findobj · fsurf · gobjects · gray · groot · hgload · hgsave · hidden · histogram2 · hold · hot · inferno · isgraphics · ishandle · jet · line · lines · linkaxes · magma · openfig · opengl · pan · parula · patch · pink · plasma · plotmatrix · plotyy · polarhistogram · polarplot · polarscatter · print · quiver3 · ribbon · saveas · savefig · sphere · spring · stackedplot · subtitle · summer · suptitle · text · textscatter · textscatter3 · triplot · turbo · viridis · waitbar · winter · wordcloud · xlabel · xlim · xline · xscale · xtickangle · xtickformat · xticklabels · xticks · ylabel · ylim · yline · yscale · ytickangle · ytickformat · yticklabels · yticks · zlim · zoom
More plotting resources
Source you can read
Every RunMat function's source is public. Read exactly how legend is executed, line by line, in Rust.
- View the source for legend in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.