【Advanced Chapter】MATLAB Multiple Plots Creation, Adjustment, and Annotation
发布时间: 2024-09-13 16:04:22 阅读量: 20 订阅数: 26
# Quick Start Tutorial Collection for MATLAB Multi-Plot Drawing
## 1. Basics of Multi-Plot Drawing in MATLAB
Multi-plot drawing in MATLAB refers to the creation of multiple graphs within a single figure window, useful for comparing different datasets, presenting various perspectives, or creating interactive visualizations. MATLAB offers a wide range of plotting functions that enable users to easily generate various types of graphs, including line plots, bar graphs, scatter plots, and surface plots.
To draw multiple plots, the `subplot` function can be used to divide the figure window into multiple sub-plot areas. Each sub-plot area can independently draw a graph. The syntax for `subplot` is `subplot(m,n,p)`, where `m` and `n` specify the size of the sub-plot grid and `p` specifies the position of the current sub-plot within the grid. For example, `subplot(2,2,1)` creates a 2x2 grid of sub-plots and selects the top-left sub-plot area.
## 2. Tips for Multi-Plot Drawing in MATLAB
### 2.1 Setting Properties of Graphical Objects
#### 2.1.1 Line Styles, Colors, and Markers
MATLAB provides a variety of line styles, colors, and marker options for customizing the visual appearance of graphical objects.
- **Line styles:** `'-'` for solid line, `'--'` for dashed line, `':'` for dotted line, `'-.'` for dash-dot line, etc.
- **Colors:** Specify using color names (e.g., `'red'`, `'blue'`) or RGB values (e.g., `[0.5, 0.5, 0.5]`).
- **Markers:** `'o'` for circle, `'x'` for cross, `'*'` for star, `'+'` for plus, etc.
```matlab
% Creating line plots with different styles, colors, and markers
figure;
x = 1:10;
y1 = sin(x);
y2 = cos(x);
y3 = tan(x);
plot(x, y1, 'r--o', 'LineWidth', 2); % Red dashed line with circle markers
hold on;
plot(x, y2, 'b-.x', 'LineWidth', 1.5); % Blue dash-dot line with cross markers
plot(x, y3, 'g:', 'MarkerSize', 10); % Green dotted line with star markers
xlabel('x');
ylabel('y');
legend('sin(x)', 'cos(x)', 'tan(x)');
grid on;
```
#### 2.1.2 Coordinate Axes and Tick Settings
MATLAB allows for flexible settings of coordinate axes and ticks to enhance the readability and aesthetics of the graph.
- **Coordinate axis labels:** Use `xlabel()` and `ylabel()` to set coordinate axis labels.
- **Tick range:** Use `xlim()` and `ylim()` to set the range of the coordinate axes.
- **Tick intervals:** Use `xticks()` and `yticks()` to set tick intervals.
- **Grid lines:** Use `grid on` or `grid off` to show or hide grid lines.
```matlab
% Setting coordinate axis labels, tick range, and grid lines
figure;
x = 0:0.1:10;
y = exp(-x);
plot(x, y);
xlabel('x');
ylabel('y');
xlim([0, 10]);
ylim([0, 1]);
xticks(0:2:10);
yticks(0:0.2:1);
grid on;
```
#### 2.1.3 Customizing Legends and Titles
Legends and titles help explain the content and purpose of the graph.
- **Legend:** Use `legend()` to add a legend, specifying the meaning of each line or marker.
- **Title:** Use `title()` to set the graph's title.
```matlab
% Adding a legend and title
figure;
x = 1:10;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'r-', 'LineWidth', 2);
hold on;
plot(x, y2, 'b--', 'LineWidth', 1.5);
legend('sin(x)', 'cos(x)');
title('Trigonometric Functions');
```
## 3.1 Scientific Data Visualization
#### 3.1.1 Drawing Line, Bar, and Scatter Plots
Drawing line, bar, and scatter plots in MATLAB is fundamental for scientific data visualization. These charts can be used to show the relationship between different variables, compare different datasets, or identify trends and patterns in the data.
**Line Plot**
A line plot connects data points with line segments to display how data changes over time or another independent variable. It is typically used to show continuous data, such as time series or experimental results.
```matlab
% Creating time series data
t = 0:0.1:10;
y = sin(t);
% Drawing a line plot
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sinusoidal Wave');
```
**Bar Plot**
A bar plot uses vertical or horizontal bars to display data for different categories or groups. It is commonly used to compare values between different groups or to show data distribution.
```matlab
% Creating category data
categories = {'A', 'B', 'C', 'D'
```
0
0