合并多个绘图 |
您所在的位置:网站首页 › 合并两个柱状图的函数 › 合并多个绘图 |
在同一坐标区中合并绘图 默认情况下,新图将清除现有图,并重置标题等坐标区属性。但是,您可以使用 hold on 命令在同一坐标区中合并多个图。例如,绘制两条直线和一个散点图,然后将 hold 状态重置为 off。 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启用 hold 状态后,新图不会清除现有图,也不会重置标题或轴标签等坐标区属性。新图将根据坐标区的 ColorOrder 和 LineStyleOrder 属性循环使用颜色和线型。坐标区范围和刻度值可能会进行调整以适应新数据。 在图窗中显示多个坐标区您可以使用 tiledlayout 函数在单个图窗中显示多个坐标区。此函数将创建一个分块图布局,该布局将图窗分为一系列不可见的图块网格。每个图块可以包含一个用于显示绘图的坐标区。创建布局后,调用 nexttile 函数将坐标区对象放置到布局中。然后调用绘图函数在该坐标区中绘图。例如,在一个 2×1 布局中创建两个绘图。为每个绘图添加标题。 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')创建跨多行或多列的绘图要创建跨多行或多列的绘图,请在调用 nexttile 时指定 span 参数。例如,创建一个 2×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)修改坐标区外观通过在每个坐标区对象上设置属性来修改坐标区外观。您可以通过带输出参数调用 nexttile 函数来获取坐标区对象。您也可以将坐标区对象指定为图形函数的第一个输入参数,以确保该函数作用于正确的坐标区。 例如,创建两个绘图,并将坐标区对象赋给变量 ax1 和 ax2。更改第一个绘图的坐标区字体大小和 x 轴颜色。为第二个绘图添加网格线。 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')控制图块周围的间距您可以通过指定 Padding 和 TileSpacing 属性来控制布局中图块周围的间距。例如,在一个 2×2 布局中显示四个绘图。 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)通过将 Padding 和 TileSpacing 属性设置为 'compact',减小布局四周和每个图块周围的间距。 t.Padding = 'compact'; t.TileSpacing = 'compact';显示共享标题和轴标签您可以在布局中显示共享标题和共享轴标签。创建一个 2×1 布局 t。然后显示一个线图和一个针状图。通过调用 linkaxes 函数来同步 x 轴范围。 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');通过将 t 传递给 title、xlabel 和 ylabel 函数,添加共享标题和共享轴标签。通过从顶部图中删除 x 轴刻度标签,并将 t 的 TileSpacing 属性设置为 'compact',让图彼此更靠近。 % 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'; |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |