Advanced Plotting Techniques
MATLAB Three-Dimensional Plotting
MATLAB three-dimensional plotting lets you display a trajectory through space or a surface whose height depends on two coordinates. In engineering, these plots help you inspect paths, geometry, and spatially varying quantities. A useful plot preserves the connection between each numerical value and its location, then makes clear what height and colour represent.
Choose a curve or a surface
A parametric curve describes coordinates using one shared parameter, often time. Its governing representation is
For one sampled trajectory, plot3(x,y,z) connects corresponding entries of three equal-length vectors in their supplied order. That ordering matters: rearranging the samples changes the connected path.
A surface instead assigns a height to each coordinate pair:
Choose the plotting function according to the relationship in your data.
Data relationship | MATLAB function | What you see |
|---|---|---|
Ordered points along one path | plot3(x,y,z) | Connected line segments in space |
Heights on a coordinate grid | mesh(X,Y,Z) | A wireframe surface |
Heights on a coordinate grid | surf(X,Y,Z) | Filled surface faces |
Separate spatial observations | scatter3(x,y,z) | Individual markers without connecting lines |
A wireframe makes the sampling pattern visible. Filled faces make broad surface shape easier to inspect, although they can hide features behind the surface.
Build and check surface coordinates
A coordinate grid contains the coordinate pair for every sampled surface point. With [X,Y] = meshgrid(x,y), each row of X repeats x, while each column of Y repeats y. If x has n entries and y has m entries, both coordinate matrices have size m-by-n.
For this grid convention,
For example, take coordinates and heights in metres, with the coefficients in the height model measured in inverse metres:
x = 0:1:3;
y = 0:2:4;
[X,Y] = meshgrid(x,y);
Z = 0.4*X.^2 + 0.2*Y.^2;
surf(X,Y,Z)
xlabel('x (m)')
ylabel('y (m)')
zlabel('Height (m)')
grid onHere, X, Y, and Z are all 3-by-4 matrices. The dots in .^2 square each coordinate separately; matrix power would perform a different operation. For this matrix-coordinate form of surf, matching sizes ensure that each height has a corresponding coordinate pair.
Control geometry, colour, and viewpoint
In surf(X,Y,Z), Z determines height and supplies the default colour data. In surf(X,Y,Z,C), Z still determines height, but C supplies a separate scalar field for colour. For example, a component's shape can provide height while temperature provides colour. Give the vertical axis and colourbar their own labels and units.
Use colorbar to display the colour scale. When comparing scalar-coloured surfaces, use the same colormap and the same clim([cmin cmax]) limits on each axes so that a given colour represents the same numerical value.
view(40,25) sets the azimuth and elevation in degrees. It changes your viewing direction, while view(2) gives a top view. axis equal gives equal displayed lengths to equal coordinate increments; use it when all three coordinates describe lengths in matching units and geometric proportions matter.
What to remember
- Use plot3 for ordered trajectories and mesh or surf for gridded surfaces.
- With meshgrid(x,y), rows follow y and columns follow x.
- Height, colour, viewpoint, and coordinate scaling have separate roles.
- Smooth appearance does not establish adequate sampling.
Example: Plot a heated sheet in three dimensions
A shaped sheet occupies 0≤x≤2.0 m and 0≤y≤1.5 m. Its height is
Its temperature is described independently by
Use a coordinate spacing of 0.5 m in both directions, including the domain endpoints.
a) Construct the coordinate and height matrices. State their dimensions and calculate H(3,4) in metres, to three decimal places.
Create the coordinate vectors, including both endpoints, then expand them into coordinate matrices:
x = 0:0.5:2.0;
y = 0:0.5:1.5;
[X,Y] = meshgrid(x,y);
H = 0.12 + 0.03*X.^2 + 0.02*Y.^2;The x vector has ________ entries and the y vector has ________. Therefore X, Y, and H are each 4-by-5, containing 20 sampled locations.
For H(3,4), row 3 selects y=1.0 m and column 4 selects x=1.5 m:
The height calculation uses matching coordinate entries, rather than treating the row index as an x coordinate.
b) Plot the sheet as a filled surface, label all three coordinate axes, and preserve its geometric proportions. Use an azimuth of 40∘ and elevation of 25∘.
Pass the coordinate matrices and the height matrix to surf. Their matching 4-by-5 dimensions associate each of the 20 heights with its physical location.
figure
surf(X,Y,H)
xlabel('x (m)')
ylabel('y (m)')
zlabel('Height (m)')
title('Sheet geometry')
grid on
axis equal
view(40,25)All three coordinates measure length in metres, so axis equal preserves equal displayed lengths for equal physical increments. The selected viewpoint helps expose the sheet's shape without changing any height values. The rectangular grid connects adjacent samples into (4−1)(5−1)=12 surface faces.
c) Display the same geometry with temperature controlling colour. Label the colourbar and calculate the temperature associated with H(3,4).
Evaluate temperature at the same coordinate pairs, producing a 4-by-5 matrix. Supply it as the fourth argument to surf so that height remains controlled by H.
T = 295 + 4*X + 2*Y;
figure
surf(X,Y,H,T)
xlabel('x (m)')
ylabel('y (m)')
zlabel('Height (m)')
title('Sheet geometry coloured by temperature')
cb = colorbar;
cb.Label.String = 'Temperature (K)';
grid on
axis equal
view(40,25)The figure shows the sampled geometry with temperature controlling face colour. Read height from the spatial axis in metres and temperature from the separate colourbar in kelvin.
At row 3, column 4, use the coordinates already identified:
The sampled vertex therefore has height 0.2075 m and temperature colour data of 303 K. The colourbar reports temperature, while the z-axis reports geometric height.
A MATLAB surface model describes the height of a machined plate. Coordinates and heights are in metres. The coefficient of x2 is 0.05 m−1, and the coefficient of y is dimensionless.
x = 0:0.2:1.2;
y = [-0.5 0 0.5 1.0];
[X,Y] = meshgrid(x,y);
Z = 0.08 + 0.05*X.^2 - 0.02*Y;
surf(X,Y,Z)What height does Z(4,6) represent? Answer in metres, to three decimal places.
A plate has temperature measurements stored in a 3-by-5 matrix Z, in kelvins. Entry Z(j,i) is the temperature measured at horizontal coordinate x(i) and vertical coordinate y(j), both in metres.
x = [0 0.5 1.0 1.5 2.0];
y = [0 0.3 0.6];An engineer wants a surface with physical x on the x-axis, physical y on the y-axis, and temperature on the z-axis. Which complete code block preserves the stated coordinate-to-temperature mapping?