# 合并多个绘图

### 在同一坐标区中合并绘图

```x = linspace(0,10,50); y1 = sin(x); plot(x,y1) title('Combine Plots') hold on y2 = sin(x/2); plot(x,y2) y3 = 2*sin(x); scatter(x,y3) hold off```

### 在图窗中显示多个坐标区

```x = linspace(0,10,50); y1 = sin(x); y2 = rand(50,1); tiledlayout(2,1) % Top plot nexttile plot(x,y1) title('Plot 1') % Bottom plot nexttile scatter(x,y2) title('Plot 2')```

### 创建跨多行或多列的绘图

```x = linspace(0,10,50); y1 = sin(x); y2 = rand(50,1); % Top two plots tiledlayout(2,2) nexttile plot(x,y1) nexttile scatter(x,y2) % Plot that spans nexttile([1 2]) y2 = rand(50,1); plot(x,y2)```

### 修改坐标区外观

```x = linspace(0,10,50); y1 = sin(x); y2 = rand(50,1); tiledlayout(2,1) % Top plot ax1 = nexttile; plot(ax1,x,y1) title(ax1,'Plot 1') ax1.FontSize = 14; ax1.XColor = 'red'; % Bottom plot ax2 = nexttile; scatter(ax2,x,y2) title(ax2,'Plot 2') grid(ax2,'on')```

### 控制图块周围的间距

```x = linspace(0,30); y1 = sin(x); y2 = sin(x/2); y3 = sin(x/3); y4 = sin(x/4); % Create plots t = tiledlayout(2,2); nexttile plot(x,y1) nexttile plot(x,y2) nexttile plot(x,y3) nexttile plot(x,y4)```

```t.Padding = 'compact'; t.TileSpacing = 'compact';```

### 显示共享标题和轴标签

```x1 = linspace(0,20,100); y1 = sin(x1); x2 = 3:17; y2 = rand(1,15); % Create plots. t = tiledlayout(2,1); ax1 = nexttile; plot(ax1,x1,y1) ax2 = nexttile; stem(ax2,x2,y2) % Link the axes linkaxes([ax1,ax2],'x');```

```% Add shared title and axis labels title(t,'My Title') xlabel(t,'x-values') ylabel(t,'y-values') % Move plots closer together xticklabels(ax1,{}) t.TileSpacing = 'compact';```