解決問題:在一個圖中畫多個子圖,又能自己確定子圖的大小和位置。
解決方法:有兩種解決方法可以實現:
- 使用subplot命令
- 使用axes函數來重新規定子圖的大小和位置。
這兩種方法中,第一種方法相對簡單,但是第二種方法更加靈活,具體使用如下:
第一種方法:使用subplot
先看一下subplot的簡單使用,程序如下:
clear; clc; close all; t = 0:0.001:10; y1 = sin(t); y2 = cos(t); figure (1); subplot(2,2,1) plot(t,y1); subplot(2,2,2) plot(t,y1); subplot(2,2,3) plot(t,y2); subplot(2,2,4) plot(t,y2);
程序運行結果:

使用subplot的這種方法,如果想要自定義子圖的大小和位置該怎么設置?
程序如下:
clear;
clc;
close all;
t = 0:0.001:10;
y1 = sin(t);
y2 = cos(t);
figure(1);
subplot('position',[0.2,0.7,0.6,0.2]);
plot(t,y1);
subplot('position',[0.2,0.2,0.6,0.2]);
plot(t,y2);
程序運行結果:

第二種方法:直接使用axes函數
程序如下:
clear;
clc;
close all;
t = 0:0.001:10;
y1 = sin(t);
y2 = cos(t);
figure (1);
axes('position',[0.1 0.6 0.3 0.3]);
plot(t,y1);
axes('position',[0.6 0.6 0.3 0.3]);
plot(t,y1);
axes('position',[0.1 0.1 0.3 0.3]);
plot(t,y2);
axes('position',[0.6 0.1 0.3 0.3]);
plot(t,y2);
程序運行結果:

其中,'position',[0.6 0.1 0.3 0.3]的含義可以參考我之前的博客文章,有詳細說明;
如果要自定義整個figure的大小,設置gcf,我之前的博客文章也有詳細說明。
