colorbar — Show, hide, or toggle colorbars in MATLAB and RunMat plotting.
colorbar controls visibility of the color scale associated with the current axes. It is axes-local and commonly used with colormap-driven plots such as imagesc, surf, and contourf, matching MATLAB behavior.
Syntax
enabled = colorbar()
enabled = colorbar(mode)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
mode | StringScalar | No | "toggle" | Colorbar mode token ('on'|'off'). |
Returns
| Name | Type | Description |
|---|---|---|
enabled | LogicalArray | Colorbar enabled state after command execution. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:colorbar:InvalidArgument | Colorbar mode argument is unsupported. | colorbar: invalid argument |
How colorbar works
colorbar onenables the colorbar for the current axes.colorbar offdisables the colorbar for the current axes.- Calling
colorbarwith no argument toggles the current colorbar state. - Colorbar visibility is subplot-local and does not automatically affect other axes.
Examples
Show a colorbar for a scaled image
[X, Y] = meshgrid(linspace(-3, 3, 60), linspace(-3, 3, 60));
A = sin(X) .* cos(Y);
imagesc(A);
colorbar on;Use colorbars independently across subplots
[X, Y] = meshgrid(linspace(-3, 3, 40), linspace(-3, 3, 40));
Z = sin(X) .* cos(Y);
subplot(1, 2, 1);
contourf(X, Y, Z);
colorbar on;
subplot(1, 2, 2);
surf(X, Y, Z);
colorbar off;Using colorbar with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how colorbar changes the result.
Run a small colorbar example, explain the result, then change one input and compare the output.
FAQ
How do I add a label to the colorbar?⌄
Use ylabel on the colorbar handle, or label the axes directly. In practice, the colorbar inherits its scale from the data range of the parent plot, and you annotate it through standard label commands on the axes.
imagesc(peaks(50));
colorbar on;
ylabel('Intensity');Can I limit the colorbar range without clipping the data?⌄
Set caxis([cmin cmax]) (or clim([cmin cmax])) on the axes to control the color mapping range. Values outside that range get clamped to the endpoint colors, but the underlying data stays intact.
Does the colorbar track across subplots automatically?⌄
No. Colorbar state is subplot-local, so each axes manages its own colorbar independently. You need to call colorbar on in each subplot where you want one visible.
Related Plotting functions
More plotting resources
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how colorbar is executed, line by line, in Rust.
- View the source for colorbar in Rust on GitHub
- Learn how the RunMat 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 blazing on any GPU. It is licensed under the Apache 2.0 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.