Main Content

여러 개의 플롯 결합하기

R2019b 이후. 다음을 대체: Combine Multiple Plots (R2019a).

이 예제에서는 hold 함수를 사용하여 동일한 좌표축에서 플롯을 결합하는 방법과 tiledlayout 함수를 사용하여 Figure에 여러 개의 좌표축을 만드는 방법을 보여줍니다.

동일한 좌표축에서 플롯 결합하기

기본적으로 새 플롯은 기존 플롯을 지우고 제목 같은 axes 속성을 재설정합니다. 그러나 hold on 명령을 사용하여 동일한 좌표축에서 여러 개의 플롯을 결합할 수 있습니다. 예를 들어, 두 개의 선과 산점도 플롯을 플로팅합니다. 그런 다음 유지 상태를 꺼짐으로 재설정합니다.

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

Figure contains an axes object. The axes object with title Combine Plots contains 3 objects of type line, scatter.

유지 상태가 켜짐이 되면 새 플롯은 기존 플롯을 지우거나 제목 또는 축 레이블 같은 axes 속성을 재설정하지 않습니다. 이 플롯은 좌표축의 ColorOrder 속성과 LineStyleOrder 속성을 기반으로 색과 선 스타일을 순서대로 돌아가며 적용합니다. 좌표축 제한과 눈금 값은 새로운 데이터를 수용하기 위해 조정될 수 있습니다.

Figure에 여러 개의 좌표축 표시하기

tiledlayout 함수를 사용하여 단일 Figure에 여러 개의 좌표축을 표시할 수 있습니다. 이 함수는 전체 Figure에 걸쳐 보이지 않는 타일 그리드를 포함하는 타일 형식 차트 레이아웃을 생성합니다. 각 타일은 플롯을 표시할 좌표축을 포함할 수 있습니다. 레이아웃을 생성한 후에는 nexttile 함수를 호출하여 axes 객체를 레이아웃에 배치합니다. 그런 다음, 플로팅 함수를 호출하여 좌표축에 플로팅합니다. 예를 들어, 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')

Figure contains 2 axes objects. Axes object 1 with title Plot 1 contains an object of type line. Axes object 2 with title Plot 2 contains an object of type scatter.

여러 행 또는 열에 걸쳐 있는 플롯 만들기

여러 행 또는 열에 걸쳐 있는 플롯을 만들려면 nexttile을 호출할 때 span 인수를 지정하십시오. 예를 들어, 2×2 레이아웃을 만들어 보겠습니다. 처음 두 개 타일에 플로팅합니다. 그런 다음 1개 행과 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)

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type scatter. Axes object 3 contains an object of type line.

좌표축 모양 수정하기

각 axes 객체의 속성을 설정하여 좌표축 모양을 수정합니다. 출력 인수를 지정하여 nexttile 함수를 호출하면 axes 객체를 가져올 수 있습니다. 또한 axes 객체를 그래픽스 함수에 대한 첫 번째 입력 인수로 지정하여 함수가 올바른 좌표축을 대상으로 하도록 할 수 있습니다.

예를 들어, 두 개의 플롯을 생성하고 axes 객체를 변수 ax1ax2에 할당합니다. 첫 번째 플롯의 좌표축 글꼴 크기와 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')

Figure contains 2 axes objects. Axes object 1 with title Plot 1 contains an object of type line. Axes object 2 with title Plot 2 contains an object of type scatter.

타일 주변의 간격 제어하기

Padding 속성과 TileSpacing 속성을 지정하여 레이아웃에서 타일 주변의 간격을 제어할 수 있습니다. 예를 들어, 2×2 레이아웃에 4개의 플롯을 표시해 보겠습니다.

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)

Figure contains 4 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line. Axes object 4 contains an object of type line.

Padding 속성과 TileSpacing 속성을 'compact'으로 설정하여 레이아웃의 둘레 주변과 각 타일 주변의 간격을 축소합니다.

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

Figure contains 4 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line. Axes object 4 contains an object of type line.

공유된 제목 및 축 레이블 표시하기

공유된 제목과 공유된 축 레이블을 레이아웃에 표시할 수 있습니다. 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');

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type stem.

ttitle, xlabel, ylabel 함수에 전달하여 공유된 제목과 공유된 축 레이블을 추가합니다. 위쪽 플롯에서 x축 눈금 레이블을 제거하고 tTileSpacing 속성을 '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';

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type stem.

참고 항목

함수

관련 항목