Matlab

What is subplot(2, 1, 1) in MATLAB

MATLAB, a powerful computational software, offers various tools for data visualization. Subplots are one such technique that enables users to build several plots within a single figure. In this article, we will explore what subplots are, learn their syntax with an example, and understand the meaning of subplot (2, 1, 1) in MATLAB.

What is a Subplot in MATLAB?

A subplot is a way to divide a MATLAB figure into a grid of smaller axes, where each axis represents an individual plot. This grid-like structure allows for the simultaneous visualization of multiple plots, aiding in data analysis, comparison, and overall visualization clarity. To create subplots in MATLAB, we use the subplot () function, which follows the syntax below:

subplot(rows, columns, index)
  • The rows indicate how many rows there are in the subplot grid.
  • The columns show how many columns there are in the subplot grid.
  • The index refers to the current position of the subplot within the grid.

Creating Subplots in MATLAB

Here is an example where we have two datasets, X and Y, and we want to display them in separate subplots within a 2-row, 1-column grid:

% Sample data

X = 1:10;

Y = X.^2;

% Create a figure with subplots

figure;

% First subplot

subplot(2, 1, 1);

plot(X, Y);

title('Plot 1');

xlabel('X');

ylabel('Y');

% Second subplot

subplot(2, 1, 2);

plot(X, sqrt(Y));

title('Plot 2');

xlabel('X');

ylabel('Square Root of Y');

% Display the figure

We start by creating a figure using the figure and then used the subplot (2, 1, 1) to specify that the first subplot should be placed in a 2-row, 1-column grid at position 1. For this subplot, we plot the dataset X versus Y and add a title, x-axis label, and y-axis label. Similarly, we create a second subplot using the subplot(2, 1, 2), plot a modified version of the data, and provide appropriate titles and labels.

What is subplot(2, 1, 1) in MATLAB?

In MATLAB, subplot(2, 1, 1) represents the first subplot in a grid with 2 rows and 1 column. It signifies that we are working with a subplot grid and the current plot is placed in the top position of the grid.

Suppose we have two datasets, A and B, and we want to display them as subplots within a 2-row, 1-column grid, below is the code to achieve this:

% Sample data

A = 1:5;

B = A.^2;

% Create a figure with subplots

figure;

% First subplot

subplot(2, 1, 1);

plot(A, B);

title('Plot 1');

xlabel('A');

ylabel('B');

Here we have created a figure with a 2-row, 1-column subplot grid using subplot (2, 1, 1). We plot the dataset A against B in the first subplot and add a title, x-axis label, and y-axis label to provide context.

A screen shot of a graph Description automatically generated with medium confidence

Conclusion

One of MATLAB’s most useful features is the ability to generate numerous plots within a single figure using subplots. By dividing the figure into a grid-like structure, subplots enable simultaneous visualization and comparison of different datasets or aspects of data.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.