MATLAB Three Dimensional Plotting Notes
Three Dimensional Plotting
- Overview: Three-dimensional plotting in MATLAB allows visualization of data points in 3D space, commonly using line plots, surface plots, and contour plots.
1. Three Dimensional Line Plots
- Input Required: Set of ordered triples (x, y, z values).
- Z-Axis: Labeled similar to x and y axes.
- Coordinate System: Uses a right-hand rule consistent with traditional mathematical conventions.
2. Surface Plots
- Surface Representation: Surface plots represent x-y-z data in a three-dimensional surface format.
- Functions to Use:
mesh for a mesh plot.surf for a colored surface plot.
- Matrix Indexing: The x and y coordinates can be treated as matrix indices (rows and columns). For instance, Y refers to rows, and X refers to columns.
- Example Code:
z = [1 10 20; 2 20 40];
mesh(z);
3. Animation Functionality
- Comet3 function: Generates an animated 3D plot of the input data.
- Usage:
comet3(x,y,z). - Tips: If the animation runs slowly, increase data points.
4. Code Examples
- To create line and animated plots:
x = linspace(0,10*pi,1000);
y = cos(x);
z = sin(x);
plot3(x,y,z);
comet3(x,y,z);
5. Surface Plot Implementation
surf(z);
surf(x,y,z);
- When using
mesh with known x and y corresponding to values of z:
mesh(x,y,z);
- Ensure that the number of columns in x matches z and the number of rows in y matches z.
6. Shading Techniques
- Shading Options:
shading interp: Interpolated shading for smoother visual transition.shading flat: Sections of the surface are displayed in flat colors.
- Color Maps: Changing the color scheme using colormap can enhance visualization:
- Examples of colormaps:
hot, cool, jet, hsv, etc. - Example code:
matlab
colormap('hot');
7. Contour Plots
- Contour Syntax: Contour plots represent data levels in 3D space using lines:
- Filled contour example:
contourf(x,y,z,20,'k').
- Usage:
[x, y, z] = peaks;
contourf(x, y, z, 20, 'k');
8. Creating 3D Plots of Spheres
- Sphere Functionality:
- Create a sphere using:
[x,y,z] = sphere(N);
surf(x,y,z);
N represents the resolution.
9. Saving and Editing Plots
- To save plots, use the
Save As... option in the MATLAB figure window, selecting a desired format (e.g., JPEG). - Interactive Edit Options: You can modify plots using the menu bar after the plot is created.
- Such adjustments include adding legends, labels, and changing aspect ratios.
Summary of Key Functions
| Function | Description |
|---|
plot3 | Create a 3D line plot |
comet3 | Animated 3D line plot |
mesh | Create a 3D mesh plot |
surf | Create a 3D surface plot |
contour | Create contour plots |
contourf | Create filled contour plots |
colormap | Change color schemes |
- Note: When adjusting a figure interactively, these changes will not be captured in the M-file unless explicitly saved to the script.