surf — Plot a surface with optional independent indexed or truecolor CData.
surf creates a surface graphics object from Z or X,Y,Z, optionally with independent color data C. The renderer preserves source geometry and color storage separately.
Syntax
h = surf(Z)
h = surf(X, Y, Z)
h = surf(Z, C)
h = surf(X, Y, Z, C)
h = surf(Z, Name, Value, ...)
h = surf(X, Y, Z, Name, Value, ...)Inputs
Z- Type
NumericArray- Required
- Yes
- Description
- Surface height grid.
X- Type
NumericArray- Required
- Yes
- Description
- X axis vector or full coordinate matrix matching Z.
Y- Type
NumericArray- Required
- Yes
- Description
- Y axis vector or full coordinate matrix matching Z.
C- Type
NumericArray- Required
- Yes
- Description
- Independent indexed m-by-n or truecolor m-by-n-by-3 surface color data.
C- Type
NumericArray- Required
- Yes
- Description
- Independent indexed m-by-n or truecolor m-by-n-by-3 color data.
props- Type
Any- Required
- Variadic
- Description
- Name/value surface style options.
Returns
h- Type
NumericScalar- Description
- Handle to the rendered surface plot.
Errors
RunMat:surf:InvalidArgument- When
- Surface/grid/axis inputs or style name/value arguments are invalid.
- Message
surf: invalid argument
RunMat:surf:Internal- When
- Internal surface construction or render preparation fails unexpectedly.
- Message
surf: internal operation failed
Surface geometry and axes
XData, YData, and ZData retain native single, double, or integer class, exact values, and shape. Vertex generation, color preparation, and rendering are explicit floating boundaries and do not replace authoritative scene data.
Surface handles can be inspected and updated with get/set. view, zlabel, colorbar, colormap, and shading use the same subplot-local axes state. surf fills and shades a surface; mesh emphasizes wireframe geometry, and surfc adds contour overlays.
When vector-axis GPU buffers can be exported through the shared plotting device, surface geometry can remain resident through the floating rendering path. Full host coordinate matrices use the CPU parametric-surface path; other unsupported direct-render combinations gather through the existing source-preparation boundary. Color limits and subplot-local metadata follow the same surface pipeline on either path. The shared surface pipeline also serves image, contour-composite, and surface-style plot families.
Coordinate and color forms
surf(Z) uses column and row indices as X and Y. surf(X,Y,Z) accepts matching coordinate grids or axis vectors. surf(Z,C) and surf(X,Y,Z,C) retain C independently of Z; C is indexed m-by-n data or truecolor m-by-n-by-3 data where [m,n]=size(Z).
Indexed CData uses the axes colormap and color limits in scaled mode. In direct mode, floating values are one-based colormap indices and integer values are zero-based indices. Floating truecolor components lie in [0,1]; NaN makes an affected vertex transparent. Integer truecolor components are normalized from their native class range only at the renderer boundary.
Graphics properties and residency
The returned graphics handle exposes CData and CDataMapping through get/set. Color data is retained with its numeric class and shape for scene replay, while rasterization converts it to renderer colors.
Explicit GPU inputs use the existing plotting-source preparation and may require a documented host gather; this plotting sink does not promise a device-resident graphics handle or an automatic elementwise GPU result. Whole-scene host allocation and browser renderer behavior still require integrated qualification.
Examples
Create a shaded surface from a height grid
[X, Y] = meshgrid(linspace(-2, 2, 80), linspace(-2, 2, 80));
Z = sin(X.^2 + Y.^2) ./ (1 + X.^2 + Y.^2);
h = surf(X, Y, Z);Pair surf with colormap, shading, and colorbar
[X, Y] = meshgrid(linspace(-3, 3, 100), linspace(-3, 3, 100));
Z = cos(X) .* sin(Y);
h = surf(X, Y, Z);
colormap('jet');
shading interp;
colorbar;Adjust camera state and z-axis labeling for a surface
[X, Y] = meshgrid(linspace(-2, 2, 60), linspace(-2, 2, 60));
Z = X .* exp(-X.^2 - Y.^2);
h = surf(X, Y, Z);
zlabel('Height');
view(45, 30);
get(h, 'Type')Expected output:
ans =
'surface'Damped radial ripple with interpolated shading
[X, Y] = meshgrid(linspace(-4, 4, 200), linspace(-4, 4, 200));
R = sqrt(X.^2 + Y.^2) + 0.01;
Z = sin(3*R) ./ R;
h = surf(X, Y, Z);
shading interp;
colormap('turbo');
colorbar;
title('Damped Radial Wavefield');
xlabel('x');
ylabel('y');
zlabel('Amplitude');
view(35, 25);Color a surface independently of height
Z = [1 2; 3 4]; C = [4 3; 2 1]; h = surf(Z,C); assert(isequal(get(h,'CData'),C));Rendered surf example
[X, Y] = meshgrid(linspace(-4, 4, 200), linspace(-4, 4, 200));
R = sqrt(X.^2 + Y.^2) + 0.01;
Z = sin(3*R) ./ R;
surf(X, Y, Z);
shading interp;
colormap('turbo');
colorbar;
title('Damped Radial Wavefield');
xlabel('x');
ylabel('y');
zlabel('Amplitude');
view(35, 25);
Compare this result with other chart types in the plot examples.
Using surf with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how surf changes the result.
Run a small surf example, explain the result, then change one input and compare the output.
FAQ
How do I change the colormap on a surf plot?⌄
Call colormap after surf to swap the palette. The colormap applies to the current axes, so it works per-subplot.
surf(X, Y, Z);
colormap('turbo');Any named colormap ('parula', 'jet', 'hot', etc.) or an Nx3 matrix of RGB values works here.
How do I rotate or set the 3-D viewing angle?⌄
Use view(az, el) where az is the azimuth in degrees and el is the elevation. For example, view(45, 30) gives you a 45-degree orbit at 30 degrees above the xy-plane. view(3) is a shortcut for the default 3-D perspective, and view(2) snaps to a top-down 2-D projection.
What's the difference between surf and mesh?⌄
surf renders a shaded, filled surface where face colors come from the colormap. mesh draws only the wireframe grid lines with no face fill. Use surf when you want to visualize the shape as a solid surface, and mesh when you want to see through the geometry to understand the grid structure underneath.
Can colors differ from surface height?⌄
Yes. Pass C as a separate m-by-n indexed grid or m-by-n-by-3 truecolor grid.
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 surf is executed, line by line, in Rust.
- View the source for surf 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.