Home > Backend Development > Python Tutorial > How to use subplot in matlab

How to use subplot in matlab

DDD
Release: 2023-11-27 14:01:32
Original
4329 people have browsed it

subplot is used in matlab to create multiple subplots in the same figure. You can draw a different graph in each subplot by specifying the number of rows, columns, and current plot position of the subplot.

How to use subplot in matlab

In MATLAB, the subplot function is used to create multiple subplots in the same figure. Its basic syntax is:

subplot(m,n,p)
Copy after login

Among them, m and n represent the number of rows and columns of the subgraph respectively, and p represents the position of the current subgraph. The subplot function divides the current figure into a matrix of m rows and n columns, and sets the current drawing position to the p-th subfigure. The following will introduce in detail how to use the subplot function.

First, we need to create a figure window, which can be created using the figure function:

figure
Copy after login

Then, we can divide the subplots through the subplot function. For example, if we want to create a subgraph matrix with 2 rows and 2 columns, and set the current drawing position to the 1st subgraph, we can use the following code:

subplot(2,2,1)
Copy after login

Next, we can Draw the graph in the figure. For example, we can use the plot function to draw a simple curve:

x = 0:0.1:2*pi;
y = sin(x);
plot(x,y)
Copy after login

Then, we can continue to create other subplots. For example, we can set the current drawing position to the 2nd subgraph and draw another curve in it:

subplot(2,2,2)
plot(x,cos(x))
Copy after login

Similarly, we can draw different graphics in other subgraphs. For example, we can set the current drawing position to the 3rd subplot and draw a scatter plot in it:

subplot(2,2,3)
x = rand(1,100);
y = rand(1,100);
scatter(x,y)
Copy after login

Finally, we can set the current drawing position to the 4th subplot and draw in it A histogram:

subplot(2,2,4)
x = 1:5;
y = [3 5 2 6 1];
bar(x,y)
Copy after login

Through these steps, we can create multiple subgraphs in the same figure window and draw different graphics in each subgraph. It is worth noting that the parameters m, n and p of the subplot function must satisfy p<=m*n, otherwise an error will occur. In addition, if we only want to draw a subplot, we can use the plot function directly instead of using the subplot function.

In short, the subplot function is a function in MATLAB used to create multiple subplots in the same figure. We can draw different graphics in each subplot by specifying the number of rows, columns, and current plot position of the subplot.

The above is the detailed content of How to use subplot in matlab. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template