matlab多个绘图,合并多个绘图- MATLAB Simulink- MathWorks 中国
在同一坐標區(qū)中合并繪圖
默認情況下,新圖將清除現有圖,并重置標題等坐標區(qū)屬性。但是,您可以使用 hold on 命令在同一坐標區(qū)中合并多個圖。例如,繪制兩條直線和一個散點圖,然后將 hold 狀態(tài)重置為 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 狀態(tài)后,新圖不會清除現有圖,也不會重置標題或軸標簽等坐標區(qū)屬性。新圖將根據坐標區(qū)的 ColorOrder 和 LineStyleOrder 屬性循環(huán)使用顏色和線型。坐標區(qū)范圍和刻度值可能會進行調整以適應新數據。
在圖窗中顯示多個坐標區(qū)
您可以使用 tiledlayout 函數在單個圖窗中顯示多個坐標區(qū)。此函數將創(chuàng)建一個分塊圖布局,該布局將圖窗分為一系列不可見的圖塊網格。每個圖塊可以包含一個用于顯示繪圖的坐標區(qū)。創(chuàng)建布局后,調用 nexttile 函數將坐標區(qū)對象放置到布局中。然后調用繪圖函數在該坐標區(qū)中繪圖。例如,在一個 2×1 布局中創(chuàng)建兩個繪圖。為每個繪圖添加標題。
注意:以下代碼使用了 tiledlayout 函數,該函數從 R2019b 開始提供。如果您使用的是較早的版本,請改用 subplot 函數。
x = linspace(0,10,50);
y1 = sin(x);
y2 = rand(50,1);
tiledlayout(2,1) % Requires R2019b or later
% Top plot
nexttile
plot(x,y1)
title('Plot 1')
% Bottom plot
nexttile
scatter(x,y2)
title('Plot 2')
創(chuàng)建跨多行或多列的繪圖
要創(chuàng)建跨多行或多列的繪圖,請在調用 nexttile 時指定 span 參數。例如,創(chuàng)建一個 2×2 布局。繪制前兩個圖塊。然后創(chuàng)建一個跨一行兩列的圖。
x = linspace(0,10,50);
y1 = sin(x);
y2 = rand(50,1);
% Top two plots
tiledlayout(2,2) % Requires R2019b or later
nexttile
plot(x,y1)
nexttile
scatter(x,y2)
% Plot that spans
nexttile([1 2])
y2 = rand(50,1);
plot(x,y2)
修改坐標區(qū)外觀
通過在每個坐標區(qū)對象上設置屬性來修改坐標區(qū)外觀。您可以通過帶輸出參數調用 nexttile 函數來獲取坐標區(qū)對象。您也可以將坐標區(qū)對象指定為圖形函數的第一個輸入參數,以確保該函數作用于正確的坐標區(qū)。
例如,創(chuàng)建兩個繪圖,并將坐標區(qū)對象賦給變量 ax1 和 ax2。更改第一個繪圖的坐標區(qū)字體大小和 x 軸顏色。為第二個繪圖添加網格線。
x = linspace(0,10,50);
y1 = sin(x);
y2 = rand(50,1);
tiledlayout(2,1) % Requires R2019b or later
% 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); % Requires R2019b or later
nexttile
plot(x,y1)
nexttile
plot(x,y2)
nexttile
plot(x,y3)
nexttile
plot(x,y4)
通過將 Padding 和 TileSpacing 屬性設置為 'none',最小化布局四周和每個圖塊周圍的間距。
t.Padding = 'none';
t.TileSpacing = 'none';
顯示共享標題和軸標簽
您可以在布局中顯示共享標題和共享軸標簽。創(chuàng)建一個 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); % Requires R2019b or later
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';
總結
以上是生活随笔為你收集整理的matlab多个绘图,合并多个绘图- MATLAB Simulink- MathWorks 中国的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php安全性差,PHP安全性防范方式
- 下一篇: matlab 如何被c 调用函数调用函数