legend — Show and configure plot legends for labeled series, display names, and MATLAB legend workflows.
legend creates or updates the legend attached to the current axes, or to an explicit axes handle when one is passed. In RunMat, legends are subplot-local and participate in the plotting object system: the builtin returns a legend handle, display names from plotted objects are used automatically, and legend styling can be inspected or updated through get and set.
How legend works in RunMat
- Calling
legendwith 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
legendworkflows. - 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
getandsetto inspect or update location, orientation, box visibility, font styling, and related legend properties.
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);
legend('left');
subplot(1, 2, 2);
plot(1:5, [5 4 3 2 1]);Expected output:
% Only the left subplot shows a legendRelated functions to explore
These functions work well alongside legend. Each page has runnable examples you can try in the browser.
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how legend works, line by line, in Rust.
- View legend.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.