skills/matlab/references/graphics-visualization.md
This reference targets MATLAB R2026a. Rendering and property support differ in GNU Octave and across MATLAB releases/platforms.
Use handles instead of relying on gcf/gca in reusable code:
fig = figure(Color="white");
layout = tiledlayout(fig, 2, 1, ...
TileSpacing="compact", ...
Padding="compact");
ax1 = nexttile(layout);
plot(ax1, time, signal, LineWidth=1.5);
xlabel(ax1, "Time (s)");
ylabel(ax1, "Amplitude (V)");
title(ax1, "Measured signal");
grid(ax1, "on");
ax2 = nexttile(layout);
histogram(ax2, residual, Normalization="pdf");
xlabel(ax2, "Residual (V)");
ylabel(ax2, "Density");
Explicit handles make tests, nested layouts, apps, and exports predictable. Set limits, aspect ratio, color limits, and view deliberately when comparison across figures matters.
Graphics functions can belong to separate products. For example, basic
plot, scatter, histogram, imagesc, surf, and tiledlayout are base
MATLAB, while domain-specific statistical, mapping, image, signal, or medical
visualizations can require named toolboxes.
exportgraphicsPrefer exportgraphics for current workflows:
exportgraphics(fig, "overview.pdf", ContentType="vector");
exportgraphics(ax1, "signal.png", Resolution=300);
R2026a-supported output includes:
SVG support was added in R2025a. Append=true is supported for PDF and GIF,
not every format. ContentType="vector" applies where supported, but some plot
content can still be rasterized. Resolution is for raster output. R2025a
added dimensions/padding controls; verify exact option and unit support in the
target release.
Interactive HTML is active web content, not a static image. Review its embedded assets and distribution context; do not open an untrusted exported HTML file automatically.
| API | Prefer for | Notes |
|---|---|---|
exportgraphics | axes, layouts, figures, publication files | current default; crop/padding, vector/raster, multipage PDF |
copygraphics | clipboard | interactive transfer; not reproducible file output |
exportapp | app/UI capture | UI-focused behavior |
print | legacy/device-specific workflows | behavior and UI support differ |
savefig | editable MATLAB figure | MATLAB object artifact, not archival interchange |
saveas | simple legacy save | less control than exportgraphics |
imwrite | image arrays/animated GIF construction | not a general figure renderer |
Never treat .fig as passive. It stores MATLAB graphics objects and should be
handled as an untrusted MATLAB object artifact unless its provenance is known.
matlab -batch starts without the desktop but can still display figure windows
unless -noFigureWindows or -nodisplay is added. Rendering may depend on
graphics hardware, fonts, installed system support, and platform. A planner
should distinguish:
The bundled command planner only returns argv and never starts MATLAB. Review trusted code, fonts, output paths, overwrite policy, and license before an approved run.
For deterministic export:
colororder(ax1, orderedColors);
colormap(ax2, "parula");
clim(ax2, [lowerLimit upperLimit]);
axis(ax2, "tight");
Use a sequential map for ordered magnitude, a diverging map around a meaningful
center, and distinct categorical colors for unordered groups. Avoid jet for
quantitative interpretation. Keep a shared color scale when panels are meant
to be compared.
Use tiledlayout/nexttile rather than new subplot code. Legends and
colorbars can belong to an axes or layout; make ownership explicit.
Many plotting functions accept tables directly. This preserves variable-name selection but does not remove the need to validate types and missing data.
plot(T, "Time", ["Observed" "Predicted"]);
legend(["Observed" "Predicted"], Location="best");
Sort time values and define duplicate/missing handling before plotting. Categorical order controls axis/group order. Avoid silently dropping missing values without reporting the count.
3-D surfaces, transparency, lighting, and very dense primitives can force rasterization or produce platform-specific output. For large data:
.fig outputs are treated as active/object artifacts.