Written and maintained by CASRAI Editorial Board
Last updated
Short answer: to plot in 3D in MATLAB, use plot3(X,Y,Z) for points or a line through 3D space, and surf(X,Y,Z) or mesh(X,Y,Z) when Z is a grid of heights over X and Y. This guide shows which one to use for your data shape, runnable examples from MathWorks documentation, the size rule behind the most common error, and how to set a reproducible camera angle with view. It covers every phrasing of the task: MATLAB 3D plot, plot 3D in MATLAB, 3D plots in MATLAB.
What 3D plotting is used for in research
Researchers use 3D plots for trajectories (a particle path, a measurement sequence over time) and for response surfaces (an outcome as a function of two parameters). Those are two different data shapes, and MATLAB has different functions for each. For the wider picture of the software, see what MATLAB is used for; for 2D figures see MATLAB graph plotting.
Install or access
MATLAB is a MathWorks product; obtain and install it only from MathWorks (mathworks.com) or through your institution’s MathWorks licence. The functions below are described in the MathWorks MATLAB documentation (checked 2026-11-07).
Which function: plot3, surf or mesh?
Choose by the shape of your data, not by how the figure should look.
| Your data | Function | What you get (per MathWorks documentation) |
|---|---|---|
| Vectors X, Y, Z of the same length (points in space) | plot3(X,Y,Z) |
Coordinates joined by line segments; matrix input draws multiple sets |
| Z as a matrix of heights, optionally with X and Y | surf(X,Y,Z) |
A three-dimensional surface with face colours |
| The same gridded Z, wireframe look | mesh(X,Y,Z) |
“Solid edge colors and no face colors” |
Sources: MathWorks documentation pages for plot3, surf and mesh, read 2026-11-07.
Step by step: plot3 for a 3D line or points
Use this when each row of your data is one point (x, y, z), such as a trajectory.
- Put your coordinates in three vectors of the same length.
- Call
plot3(X,Y,Z); add a line specification as a fourth argument,plot3(X,Y,Z,LineSpec), to change the style. - Label the axes with
xlabel,ylabelandzlabel.
The helix example from the MathWorks plot3 page:
t = 0:pi/50:10*pi;st = sin(t);ct = cos(t);plot3(st,ct,t)
With a table, the documented form is plot3(tbl,xvar,yvar,zvar), and p = plot3(___) returns the line object so you can change its properties afterwards.
Step by step: surf and mesh for a surface
Use these when you have a value Z at every point of an X-Y grid, for example a model evaluated over two parameters.
- Build matching grids with
meshgrid. - Compute Z from the grids.
- Call
surf(X,Y,Z)for a coloured surface, ormesh(X,Y,Z)for a wireframe. - Add
colorbarto show the colour scale.
The MathWorks surf example:
[X,Y] = meshgrid(1:0.5:10,1:20);Z = sin(X) + cos(Y);surf(X,Y,Z)colorbar
The MathWorks mesh example:
[X,Y] = meshgrid(-8:.5:8);R = sqrt(X.^2+Y.^2)+eps;Z = sin(R)./R;mesh(X,Y,Z)
Documented variations: surf(Z) and mesh(Z) use the column and row indices as X and Y; surf(Z,C) and surf(X,Y,Z,C) colour the surface by C, where C is an m-by-n array of colormap indices or an m-by-n-by-3 array of RGB values; name-value arguments such as FaceAlpha=0.5 make the surface translucent; s = surf(___) returns the surface object. EdgeColor and FaceColor accept "flat" and "interp". For colormap choices see MATLAB colormap colours.
Set the viewing angle with view
view(az,el) sets the camera. Azimuth az is the angle in degrees from the negative y-axis, increasing counterclockwise about the z-axis viewed from above. Elevation el is the angle in degrees between the line of sight and the x-y plane, from -90 to 90. Two shortcuts are documented: view(2) is the default 2D view (az = 0, el = 90, looking straight down) and view(3) is the default 3D view (az = -37.5, el = 30). Stating the two angles in your script makes a figure reproducible, instead of depending on mouse rotation. [caz,cel] = view returns the current angles. Source: MathWorks view page, read 2026-11-07.
Output and how to read it
- plot3 draws a single connected line; the order of your points is the order of the path.
- surf and mesh draw the height Z over the X-Y grid. With
surf(X,Y,Z)the colour follows height unless you supply C, which is whycolorbaris worth adding. - Rows of Z run along Y and columns along X (see the size rule below).
Troubleshooting
Z must have at least two rows and two columns
The surf documentation states that Z must have at least two rows and two columns. A single vector cannot be drawn as a surface; use plot3 for it.
X, Y and Z sizes do not match
X and Y may be matrices the same size as Z, or vectors. If Z is m-by-n, the vector X must have length n (columns) and Y length m (rows). The simplest fix is to generate X and Y from meshgrid, which returns matching matrices. If you omit X and Y, MATLAB uses 1:n and 1:m.
I want a surface from scattered points
The surface functions above expect gridded Z. Scattered (x, y, z) triples are not a grid, and plot3 joins points in the order given, which is rarely what you want for unordered data. Arrange the data on a regular X-Y grid first.
The plot looks flat or from the wrong side
Set the camera explicitly with view(az,el) as above.
Free alternatives
| Tool | 3D line / points | 3D surface | Source |
|---|---|---|---|
| GNU Octave (manual for version 11.3.0) | plot3 “displays arbitrary three-dimensional data, without requiring it to form a surface” |
mesh (and meshc, with contours), built on meshgrid |
docs.octave.org, read 2026-11-07 |
| Python Matplotlib (3.11.2 docs) | See the mplot3d toolkit documentation | Axes3D.plot_surface(X, Y, Z) creates a surface plot |
matplotlib.org, read 2026-11-07 |
For the R route see 3D plots in R; for Python contour views see contour plots in Matplotlib.
FAQ
How do I plot 3D in MATLAB?
Use plot3(X,Y,Z) for a line or points in 3D, or surf(X,Y,Z) / mesh(X,Y,Z) for a surface over a grid.
What is the difference between surf and mesh?
mesh draws solid edge colours with no face colours; surf draws a surface with face colours (MathWorks documentation).
How do I make a 3D plot from x, y and z vectors?
If the vectors are points along a path, plot3(x,y,z). If you want a surface, build a grid with [X,Y] = meshgrid(x,y) and compute Z on it.
How do I rotate or fix the angle of a 3D plot?
Call view(az,el) with azimuth and elevation in degrees, or view(3) for the default 3D view.
Sources
- MathWorks, plot3, surf, mesh, view (read 2026-11-07).
- GNU Octave documentation, Three-Dimensional Plots (read 2026-11-07).
- Matplotlib documentation, Axes3D.plot_surface (read 2026-11-07).







