Plotting and Visualization
## Learning Objectives
- Create 2D plots
- Customize plot appearance
- Create 3D plots
- Export and save figures
## Basic 2D Plotting
### Simple Line Plot
```matlab
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y)
```
### Multiple Lines
```matlab
x = 0:0.1:2*pi;
plot(x, sin(x))
hold on
plot(x, cos(x))
hold off
```
### Plotting Functions
```matlab
fplot(@(x) sin(x), [0, 2*pi])
fplot(@(x) x^2 - 2*x + 1, [-5, 5])
```
## Plot Customization
### Line Properties
```matlab
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y, 'r-', 'LineWidth', 2) % Red, solid, 2pt width
```
### Color, Marker, Line Style
| Code | Property | Code | Property | Code | Property | Code | Property |
|------|----------|------|----------|------|----------|------|----------|
| `b` | Blue | `g` | Green | `r` | Red | `c` | Cyan |
| `m` | Magenta | `y` | Yellow | `k` | Black | `w` | White |
| `-` | Solid | `--` | Dashed | `:` | Dotted | `-.` | Dash-dot |
| `.` | Point | `o` | Circle | `x` | X-mark | `+` | Plus |
| `*` | Star | `s` | Square | `d` | Diamond | `^` | Triangle |
### Markers
```matlab
plot(x, y, 'ro-') % Red circles with line
plot(x, y, 'b--') % Blue dashed
plot(x, y, 'gs') % Green squares
```
## Axis and Labels
### Axis Labels
```matlab
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y)
xlabel('x (radians)')
ylabel('sin(x)')
title('Sine Function')
```
### Axis Limits
```matlab
axis([xmin, xmax, ymin, ymax])
xlim([0, 10])
ylim([-2, 2])
```
### Grid and Box
```matlab
grid on
grid off
grid minor
box on
box off
```
### Axis Equal
```matlab
axis equal % Same scale on both axes
axis square % Square plot area
```
## Multiple Plots
### Subplots
```matlab
x = 0:0.1:2*pi;
subplot(2, 2, 1)
plot(x, sin(x))
title('Sine')
subplot(2, 2, 2)
plot(x, cos(x))
title('Cosine')
subplot(2, 2, 3)
plot(x, tan(x))
title('Tangent')
subplot(2, 2, 4)
plot(x, exp(x))
title('Exponential')
```
### Using hold
```matlab
plot(x, sin(x))
hold on
plot(x, cos(x))
hold off
legend('sin', 'cos')
```
## Specialized 2D Plots
### Scatter Plot
```matlab
x = rand(50, 1) * 10;
y = rand(50, 1) * 10;
colors = rand(50, 1);
scatter(x, y, 50, colors, 'filled')
colorbar
```
### Bar Plot
```matlab
categories = {'A', 'B', 'C', 'D'};
values = [15, 30, 25, 10];
bar(categories, values)
ylabel('Count')
% Stacked bar
bar(categories, [15, 30; 25, 10], 'stacked')
```
### Histogram
```matlab
data = randn(1000, 1);
histogram(data, 30) % 30 bins
histogram(data, 'Normalization', 'pdf')
```
### Pie Chart
```matlab
sizes = [30, 25, 20, 15, 10];
labels = {'A', 'B', 'C', 'D', 'E'};
pie(sizes, labels)
```
### Box Plot
```matlab
data = randn(100, 4);
boxplot(data, 'Labels', {'A', 'B', 'C', 'D'})
ylabel('Value')
```
### Error Bars
```matlab
x = 1:5;
y = [10, 15, 12, 18, 20];
errors = [1, 2, 1.5, 2.5, 1];
errorbar(x, y, errors)
```
## 3D Plotting
### 3D Line Plot
```matlab
t = 0:0.1:10*pi;
x = cos(t);
y = sin(t);
z = t;
plot3(x, y, z)
xlabel('x')
ylabel('y')
zlabel('z')
title('Helix')
```
### Surface Plot
```matlab
[x, y] = meshgrid(-2:0.1:2, -2:0.1:2);
z = x.^2 + y.^2;
surf(x, y, z)
xlabel('x')
ylabel('y')
zlabel('z')
colorbar
```
### Mesh Grid
```matlab
[x, y] = meshgrid(-2:0.5:2, -3:0.5:3);
% x and y are 2D grids for 3D plotting
```
### Contour Plot
```matlab
[x, y] = meshgrid(-2:0.1:2, -2:0.1:2);
z = x.^2 + y.^2;
contour(x, y, z, 20) % 20 contour levels
contourf(x, y, z, 20) % Filled contours
```
### 3D Scatter
```matlab
x = rand(100, 1) * 10;
y = rand(100, 1) * 10;
z = rand(100, 1) * 10;
colors = rand(100, 1);
scatter3(x, y, z, 50, colors, 'filled')
```
### Mesh and Surface Combined
```matlab
[x, y] = meshgrid(-3:0.2:3);
z = peaks(x, y);
mesh(x, y, z)
hold on
surf(x, y, z)
hold off
```
## Plot Styling
### Colormaps
```matlab
colormap jet
colormap parula
colormap gray
colormap hot
colormap cool
colormap viridis
```
### Lighting and View
```matlab
[x, y] = meshgrid(-2:0.1:2);
z = peaks(x, y);
surf(x, y, z)
shading interp
lighting gouraud
material metal
camlight
view(45, 30) % Azimuth, elevation
```
### Transparency
```matlab
[x, y] = meshgrid(-2:0.2:2);
z = x.^2 + y.^2;
surf(x, y, z, 'FaceAlpha', 0.5) % Semi-transparent
```
## Annotations
### Text
```matlab
plot(0:0.1:2*pi, sin(0:0.1:2*pi))
text(pi, 0, 'sin(\pi) = 0', 'FontSize', 12)
```
### Arrows and Shapes
```matlab
annotation('arrow', [0.2, 0.4], [0.8, 0.6])
annotation('rectangle', [0.2, 0.2, 0.1, 0.1])
annotation('line', [0.1, 0.3], [0.5, 0.5])
```
## Figure Management
### Creating Figures
```matlab
figure(1)
plot(x, y)
figure(2)
plot(x, z)
```
### Saving Figures
```matlab
% Save as image
saveas(gcf, 'plot.png')
saveas(gcf, 'plot.jpg')
saveas(gcf, 'plot.pdf')
saveas(gcf, 'plot.fig')
% Export for editing
print('-djpeg', 'plot.jpg')
print('-depsc', 'plot.eps')
```
### Closing Figures
```matlab
close % Close current figure
close all % Close all figures
close(2) % Close figure 2
```
## Multiple Axes
```matlab
x = 0:0.1:10;
yyaxis left
plot(x, sin(x))
ylabel('Sin')
yyaxis right
plot(x, exp(x/5))
ylabel('Exp')
```
## Animations
### Simple Animation
```matlab
x = 0:0.1:10;
figure
for i = 1:length(x)
plot(x(1:i), sin(x(1:i)))
xlim([0, 10])
ylim([-1, 1])
drawnow
end
```
## Summary
- `plot()` for basic 2D line plots
- Use `'Color', 'Marker', 'LineStyle'` for customization
- `xlabel()`, `ylabel()`, `title()`, `legend()` for labels
- `subplot()` for multiple plots in one figure
- `surf()`, `mesh()` for 3D surface plots
- `scatter()`, `bar()`, `histogram()` for specialized plots
- `colormap()` to change colors
- `saveas()` or `print()` to export figures
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →