subplot
Divides a figure into a grid and selects one cell for the next plot.
Category: Graphics > Formatting and Annotation
Syntax
subplot(m, n, p)
Splits the figure into an m-by-n grid and makes cell p current, counting along rows from the top left.
subplot(m, n, [p1 p2])
Makes one axes that spans several cells.
subplot(mnp)
Shorthand with three digits, so subplot(223) means subplot(2,2,3).
subplot(ax)
Makes an existing axes current.
Description
After subplot(m, n, p), plotting commands draw in that cell. Calling subplot again with a different p moves to another cell without disturbing what was drawn before. Each cell is its own axes, with its own title, labels and limits.
The newer tiledlayout and nexttile give tighter control of spacing and shared titles, but subplot remains the most common way to arrange a few plots.
Input arguments
m, n- Number of rows and columns in the grid.
p- Cell index, or a vector of indices to span several cells.
Example
Three cells, one spanning two
x = linspace(0, 2*pi, 100);
subplot(2, 2, 1), plot(x, sin(x)), title('sin')
subplot(2, 2, 2), plot(x, cos(x)), title('cos')
subplot(2, 2, [3 4]), plot(x, sin(x).*cos(x)), title('product')
In the app, the figure is drawn below the code.