MATLAB 绘图笔记
MATLAB的语法学习曲线很特别:入门时用起来特别简单舒服,但是深入学下去就会感觉到语法非常混乱,无所适从。 尤其在绘图部分,MATLAB杂揉了命令式语句和面向对象的语句,存在太多的语法糖,毫无逻辑性可言。 记录一些关于绘图的代码吧,便于查找。
绘制多条曲线
绘制多条曲线 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(2*x);
figure;
hold on;
plot(x, y1, 'r-', LineWidth=2, DisplayName='sin(x)');
plot(x, y2, 'b--', LineWidth=2, DisplayName='cos(x)');
plot(x, y3, 'g:', LineWidth=2, DisplayName='sin(2x)');
hold off;
title('Multiple Curves with Direct Legend');
xlabel('x');
ylabel('Function Values');
grid on;
axis([0 2*pi -1.5 1.5]);
% or xlim([xmin xmax])
legend();
这里:
legend()展示图例:可以在legend函数中提供,也可以使用DisplayName参数提供;title设置标题;xlabel,ylabel:设置轴标签。grid on:显示网格线;axis或xlim:指定轴范围
plot 曲线样式
plot
函数支持多种曲线样式,包括颜色、线型、标记符号、线宽等,下面记录一下细节。
线型 LineStyle
| 线型 | 符号 | 示例 |
|---|---|---|
| 实线 | '-' |
plot(x, y, LineStyle='-') |
| 虚线 | '--' |
plot(x, y, LineStyle='--') |
| 点线 | ':' |
plot(x, y, LineStyle=':') |
| 点划线 | '-.' |
plot(x, y, LineStyle='-.') |
颜色 Color
| 颜色 | 符号 | 示例 |
|---|---|---|
| 红色 | 'r' |
plot(x, y, Color='r') |
| 绿色 | 'g' |
plot(x, y, Color='g') |
| 蓝色 | 'b' |
plot(x, y, Color='b') |
| 青色 | 'c' |
plot(x, y, Color='c') |
| 洋红 | 'm' |
plot(x, y, Color='m') |
| 黄色 | 'y' |
plot(x, y, Color='y') |
| 黑色 | 'k' |
plot(x, y, Color='k') |
| 白色 | 'w' |
plot(x, y, Color='w') |
| RGB 颜色 | plot(x, y, Color=[0,0.7,0.9]) |
标记 Marker
| 标记 | 符号 | 示例 |
|---|---|---|
| 圆圈 | 'o' |
plot(x, y, Marker='o') |
| 加号 | '+' |
plot(x, y, Marker='+') |
| 星号 | '*' |
plot(x, y, Marker='*') |
| 点 | '.' |
plot(x, y, Marker='.') |
| 叉号 | 'x' |
plot(x, y, Marker='x') |
| 方块 | 's' |
plot(x, y, Marker='s') |
| 菱形 | 'd' |
plot(x, y, Marker='d') |
| 向上三角 | '^' |
plot(x, y, Marker='^') |
| 向下三角 | 'v' |
plot(x, y, Marker='v') |
| 向左三角 | '<' |
plot(x, y, Marker='<') |
| 向右三角 | '>' |
plot(x, y, Marker='>') |
| 五角星 | 'p' |
plot(x, y, Marker='p') |
| 六边形 | 'h' |
plot(x, y, Marker='h') |
如果每一个点都显示标记符号,就显得太拥挤了,可以使用
MarkerIndices选项对数据采样后显示标记。
线型+颜色+标记
由于线型+颜色+标记这三个属性太常用了,MATLAB支持一次性使用字符数组来配置。
例如 1
plot(x, y, 'r--o');
'r--o' 对应为:
Color='r'红色LineStyle='--'虚线Marker='o'圆圈
三者顺序并不固定。
例如 1
2
3
4
5
6
7
8
9
10
11x = linspace(0, 10, 40);
y1 = sin(x);
y2 = cos(x);
figure;
plot(x, y1, 'ro-'); % 红色 + 圆点 + 实线
hold on;
plot(x, y2, 'bs--'); % 蓝色 + 方块 + 虚线
hold off;
legend('sin(x)', 'cos(x)');
grid on;

其他绘图属性
| 属性 | 作用 | 示例 |
|---|---|---|
'LineWidth' |
线宽 | LineWidth=2 |
'MarkerSize' |
标记大小 | MarkerSize=8 |
'MarkerEdgeColor' |
标记边缘颜色 | MarkerEdgeColor='k' |
'MarkerFaceColor' |
标记填充颜色 | MarkerFaceColor='r' |
'MarkerIndices' |
标记显示索引 | MarkerIndices=1:5:length(y) |
例如 1
2
3
4
5
6
7x = linspace(0, 10, 40);
y = sin(x);
figure;
plot(x, y, 'ro', MarkerSize=10, MarkerEdgeColor='b', MarkerFaceColor='y');
grid on;
title('Customized Markers');
绘图窗口保持
默认情况下,绘制的曲线图呈现在一个绘图窗口中,但是再次调用plot函数绘图时,MATLAB
会自动清空已打开的绘图窗口,重新绘制。
如果我们在同一窗口中希望把旧图像保持,在此基础上继续绘图,需要使用hold on命令。hold on命令会使得当前的绘图窗口的内容保持住,
直到使用hold off命令或者关闭绘图窗口。
例如 1
2
3
4
5
6
7
8
9
10
11
12x = linspace(0,2*pi);
y = sin(x);
figure;
plot(x,y)
hold on
y2 = cos(x);
plot(x,y2)
hold off
但是需要特别注意的是,
hold on会影响后续绘图的布局,将其固定为当前布局,例如调用对数坐标绘图时,不能先执行hold on;
多个绘图窗口
我们可以多次调用figure;语句来创建不同的绘图窗口,分别在其中进行绘图
1 | x = linspace(0, 30); |
多个子图
有很多种方式可以创建子图,下面分别举例。
基于subplot实现 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21x = linspace(0, 30);
figure;
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, sec(x));
title('Secant');
sgtitle('Trigonometric Functions');
基于titledlayout实现
1 | t = tiledlayout(2,2); % 2*2的布局 |
对数/双对数图
与 plot 不同,可以使用
semilogx、semilogy
把单个坐标轴改成对数坐标,也可以使用 loglog
使用双对数坐标。
1 | t = linspace(-1,2,100); |
效果如下图

图片保存
MATLAB提供了很多种保存绘图的方式,我们重点关注其中的两种:
savefig保存的是 MATLAB 图形的完整信息,以.fig格式保存,可在 MATLAB 中重新加载和编辑,但是不能使用图片浏览器打开;saveas保存的是 MATLAB 图形的静态图像,以具体图片格式保存,如 PNG、EPS、JPEG 等,无法重新编辑。
使用savefig可以直接保存绘图窗口到文件中,只需要提供文件名
1
2
3
4
5
6
7
8
9x = linspace(0, 30);
figure;
plot(x, sin(x));
title('Sine');
xlabel('x');
ylabel('sin(x)');
savefig("temp.fig")
也可以保存指定的figure句柄 1
2
3
4
5
6
7
8
9x = linspace(0, 30);
fig1 = figure;
plot(x, sin(x));
title('Sine');
xlabel('x');
ylabel('sin(x)');
savefig(fig1,"temp.fig")
我们可以在MATLAB中直接点击.fig文件来打开它,也可以在代码中使用openfig来打开
1
openfig("temp.fig")
使用saveas可以保存为某个具体的图片格式,例如
1
2fig = openfig('example.fig');
saveas(fig, 'example_saved.png');
或者 1
2
3
4
5
6
7
8
9x = linspace(0, 30);
fig1 = figure;
plot(x, sin(x));
title('Sine');
xlabel('x');
ylabel('sin(x)');
saveas(fig1, 'example_saved.png');
注意:
- 必须为
saveas函数提供figure句柄; - 保存图片的格式会根据文件后缀名推测,例如
xxx.png和xxx.jpg。
补充
MATLAB 在绘图时提供了很多指令式的语句,例如 1
2hold on
grid on
但是它们其实也有等效的函数调用写法,可读性明显更好,但是似乎没什么人用。
1
2hold('on')
grid('on')