0
0
MATLABdata~5 mins

Subplot for multiple panels in MATLAB

Choose your learning style9 modes available
Introduction

Subplots let you show many graphs in one window. This helps compare data easily.

You want to compare sales trends for different products side by side.
You need to show temperature changes over days for multiple cities in one figure.
You want to display different parts of a dataset in separate small graphs.
You are analyzing multiple experiments and want to see their results together.
Syntax
MATLAB
subplot(rows, columns, index)
plot(data)
title('Title')

rows and columns set how many panels in vertical and horizontal directions.

index picks which panel you are drawing in, counting left to right, top to bottom.

Examples
Creates a 2 by 2 grid and draws the first plot in the top-left panel.
MATLAB
subplot(2, 2, 1)
plot(x, y1)
title('Panel 1')
Creates 3 rows and 1 column, draws plot in the second panel (middle).
MATLAB
subplot(3, 1, 2)
plot(x, y2)
title('Middle Panel')
Creates 1 row and 3 columns, draws plot in the last panel on the right.
MATLAB
subplot(1, 3, 3)
plot(x, y3)
title('Right Panel')
Sample Program

This code creates one figure with three panels stacked vertically. Each panel shows a different function of x.

MATLAB
x = 1:10;
y1 = x;
y2 = x.^2;
y3 = sqrt(x);

figure;
subplot(3,1,1);
plot(x, y1);
title('Linear');

subplot(3,1,2);
plot(x, y2);
title('Square');

subplot(3,1,3);
plot(x, y3);
title('Square Root');
OutputSuccess
Important Notes

Use figure before subplot to start a new window.

You can customize each subplot with titles, labels, and legends separately.

Panels are counted left to right, top to bottom for the index.

Summary

Subplots help show multiple graphs in one figure for easy comparison.

Use subplot(rows, columns, index) to select the panel.

Each panel can have its own plot and title.