RunMat
  • Pricing
RunMat
GitHub
GitHub
Sign inTry in browser
DesktopRuntimePhysicsCADAdministrationAgent

Get RunMat updates

New releases, guides and examples.

Product+
  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download
Math+
  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting
Physics+
  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA
Industries+
  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance
Learn+
  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students
Compare+
  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons
Company+
  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms

Product

  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download

Math

  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting

Physics

  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA

Industries

  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance

Learn

  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students

Compare

  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons

Company

  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms
GitHubXLinkedIn

© Dystr, Inc. · RunMat v0.7.1, Nov 17, 2026

RunMat® is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
See all docs
Builtin Reference
    • addpoints
    • ancestor
    • animatedline
    • area
    • autumn
    • axes
    • axis
    • bar
    • barh
    • bone
    • box
    • caxis
    • cla
    • clf
    • clim
    • colorbar
    • colorcube
    • colormap
    • colororder
    • contour
    • contour3
    • contourf
    • cool
    • copper
    • copyobj
    • cylinder
    • daspect
    • datacursormode
    • dataTipTextRow
    • drawnow
    • errorbar
    • fcontour
    • figure
    • fill
    • fill3
    • findobj
    • fsurf
    • gca
    • gcf
    • get
    • gobjects
    • gray
    • grid
    • groot
    • heatmap
    • hgload
    • hgsave
    • hidden
    • hist
    • histogram
    • histogram2
    • hold
    • hot
    • image
    • imagesc
    • imshow
    • inferno
    • isgraphics
    • ishandle
    • jet
    • legend
    • line
    • lines
    • linkaxes
    • loglog
    • magma
    • mesh
    • meshc
    • openfig
    • opengl
    • pan
    • parula
    • patch
    • pie
    • pink
    • plasma
    • plot
    • plot3
    • plotmatrix
    • plotyy
    • polarhistogram
    • polarplot
    • polarscatter
    • print
    • quiver
    • quiver3
    • ribbon
    • saveas
    • savefig
    • scatter
    • scatter3
    • semilogx
    • semilogy
    • set
    • sgtitle
    • shading
    • sphere
    • spring
    • stackedplot
    • stairs
    • stem
    • subplot
    • subtitle
    • summer
    • suptitle
    • surf
    • surfc
    • text
    • textscatter
    • textscatter3
    • title
    • triplot
    • turbo
    • view
    • viridis
    • waitbar
    • winter
    • wordcloud
    • xlabel
    • xlim
    • xline
    • xscale
    • xtickangle
    • xtickformat
    • xticklabels
    • xticks
    • ylabel
    • ylim
    • yline
    • yscale
    • ytickangle
    • ytickformat
    • yticklabels
    • yticks
    • zlabel
    • zlim
    • zoom

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.
NameTypeRequiredDefaultDescription
modeStringScalarYes—Legend mode token: 'on'|'off'|'show'|'hide'|'boxon'|'boxoff'.
labels_or_pairsAnyVariadic—Labels (string/cell/string-array) with optional trailing Name/Value legend style pairs.
axAxesHandleYes—Target axes handle.

Returns

h
Type
NumericScalar
Description
Legend object handle.
NameTypeDescription
hNumericScalarLegend 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
IdentifierWhenMessage
RunMat:legend:InvalidArgumentLegend arguments are malformed, unsupported, or have invalid labels/properties.legend: invalid argument
RunMat:legend:InvalidAxesResolved axes target is invalid or out of range.legend: invalid axes target
RunMat:legend:InternalInternal legend state operation fails.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

3D & Surface

contour · contour3 · contourf · mesh · meshc · plot3 · quiver · scatter3 · surf · surfc

Images

image · imagesc · imshow

Axes & Layout

axis · box · grid · sgtitle · subplot · title · view · zlabel

Appearance

colorbar · colormap · shading

Handle Access

gca · gcf · get · set

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

  • Choosing the right plot type
  • Styling plots and axes
  • Plot replay and export
  • Complete plotting guide

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • Behavior
  • Placement and effects
  • Examples
  • Build a legend from display names
  • Pass labels explicitly and position the legend
  • Keep legends subplot-local
  • Using legend with coding agents
  • FAQ
  • Related Plotting functions
  • 2D Charts
  • 3D & Surface
  • Images
  • Axes & Layout
  • Appearance
  • Handle Access
  • Other
  • More plotting resources
  • Source you can read
  • About RunMat