
3.2 figure函数与subplot函数
在MATLAB中,figure函数用于创建一个新的图形对象。图形对象会在屏幕上单独的窗口中输出。
subplot函数用于生成与控制多个坐标轴。把当前图形窗口分隔成几个矩形部分,不同的部分是按行方向以数字进行标号的。每一部分有一坐标轴,后面的图形输出于当前的部分中。subplot函数的用法有以下几种:
(1)h=subplot(m,n,p)/subplot(mnp):将figure划分为m×n块,在第p块创建坐标系,并返回它的句柄。当m、n、p<10时,可以简化为subplot(mnp)或者subplot mnp。subplot是将多个图画到同一个平面上的工具。其中,m表示是图排成m行,n表示图排成n列,也就是整个figure中有n个图是排成一行的,一共m行,如果第一个数字是2就是表示2行图。p是指要把曲线画到figure中哪个图上,如果是1表示是从左到右第一个位置。
(2)subplot(m,n,p,'replace'):创建新坐标系来替换已存在的所指定的坐标系。
(3)subplot(m,n,P):此时P为向量,表示将P中指定的小块合并成一个大块创建坐标系,P中指定的小块可以不连续,甚至不相连。比如subplot(2,3,[2 5])表示将第2和5小块连成一个大块;subplot(2,3,[2 6])由于2和6不连续也不相连,此时表示将第2、3、5和6四块连成一个大块,相当于subplot(2,3,[2 3 5 6])。
(4)subplot(h):将坐标系h设为当前坐标系,相当于axes(h)。
(5)subplot('Position',[left bottom width height]):在指定位置创建一个新坐标系,等效于axes('Position',[left bottom width height])。
(6)subplot(...,prop1,value1,prop2,value2,...):在创建坐标系时,同时设置相关属性。
(7)h=subplot(...):返回所创建坐标系的句柄。
【例3-6】 画出参数方程的图形。
程序命令如下:

运行结果如图3-6所示。

图3-6 参数方程的图形
【例3-7】 利用subplot(m,n,P)函数对图形进行分割。
程序命令如下:

运行结果如图3-7所示。

图3-7 均匀分割

运行结果如图3-8所示。

图3-8 左右分割