You can create various types of 3D graphs in Matlab using functions like mesh
, surf
, surfl
, and contour
with sample data generated by functions such as peaks
.
Matlab offers powerful tools for visualizing data in three dimensions, which is essential for understanding complex relationships and surfaces. The choice of function depends on the type of 3D representation you need. Below are some common methods and examples based on the provided references.
Common 3D Plot Types
Here are some fundamental ways to generate 3D plots in Matlab, illustrated with simple examples. Often, you start with a 2D matrix z
representing the height or value at each point on a grid (x
, y
).
Mesh Plots
Mesh plots display a 3D surface as a wireframe grid. The color of the mesh lines represents the surface's height.
-
Example Code:
z = peaks(25); figure mesh(z)
-
Explanation:
z = peaks(25)
: Generates a sample 25x25 matrixz
using thepeaks
function, which is a standard test function with multiple peaks and valleys.figure
: Creates a new figure window to display the plot.mesh(z)
: Creates a mesh plot of the data in the matrixz
.
Surface Plots
Surface plots display a 3D surface with colored faces and black edges (by default). The face colors represent the surface's height.
-
Example Code:
z = peaks(25); figure surf(z)
-
Explanation:
z = peaks(25)
: Same as the mesh example, generating sample data.figure
: Creates a new figure window.surf(z)
: Creates a surface plot of the data in the matrixz
.
Shaded Surface Plots
Shaded surface plots are similar to standard surface plots but often use lighting effects to create a more realistic appearance. You can also customize the colormap and shading.
-
Example Code:
z = peaks(25); figure surfl(z) colormap(pink) % change color map shading interp % interpolate colors across lines and faces.
-
Explanation:
z = peaks(25)
: Generates sample data.figure
: Creates a new figure window.surfl(z)
: Creates a shaded surface plot with lighting effects.colormap(pink)
: Applies the built-in 'pink' colormap to the plot.shading interp
: Interpolates colors smoothly across the surface faces and edges, resulting in a less blocky look compared to the default flat shading.
Contour Plots
Contour plots represent a 3D surface by drawing 2D curves (contour lines) that connect points of equal height. While often 2D, they visualize 3D data and can also be drawn in 3D space (contour3
). The reference shows a 2D version.
-
Example Code:
z = peaks(25); figure contour(z,16) colormap default % change color map.
-
Explanation:
z = peaks(25)
: Generates sample data.figure
: Creates a new figure window.contour(z, 16)
: Creates a 2D contour plot of the data inz
, drawing 16 contour levels.colormap default
: Resets the colormap to the system's default.
Customization Options
Matlab offers extensive options to customize your 3D plots:
- Colormaps: Use the
colormap
function with various built-in options (e.g.,'jet'
,'parula'
,'pink'
) or custom maps. - Shading: Use the
shading
function (e.g.,'flat'
,'interp'
,'faceted'
) to control how colors are applied to the surface. - Labels and Title: Add labels to the x, y, and z axes using
xlabel
,ylabel
, andzlabel
, and a title usingtitle
. - Viewpoint: Change the camera angle using
view
. - Lighting: For plots created with functions like
surfl
, you can adjust lighting properties.
Summary Table
Function | Description | Typical Appearance |
---|---|---|
mesh |
Wireframe grid | Lines connect data points |
surf |
Surface with colored faces and edges | Solid appearance |
surfl |
Surface with lighting effects | Shaded, often smoother |
contour |
2D representation using contour lines | Lines of equal height in 2D |
For more detailed information on creating 3D plots, refer to the official MathWorks documentation: Creating 3-D Plots