Home  >  Article  >  How to draw three-dimensional graphs in matlab

How to draw three-dimensional graphs in matlab

小老鼠
小老鼠Original
2024-04-13 00:33:17670browse

There are many ways to draw three-dimensional graphics in MATLAB: use the plot3 function to draw line graphs. Use the mesh function to draw mesh surfaces. Use the surf function to draw a colored surface. Use the scatter3 function to draw a scatter plot. Plot a histogram using the histogram3 function.

How to draw three-dimensional graphs in matlab

How to draw three-dimensional graphics in MATLAB

In MATLAB, you can use various functions to draw three-dimensional graphics. Several common methods are listed below:

1. Use the plot3 function

plot3 function for plotting 3D line graph. It accepts three vectors as input, representing x, y and z coordinates respectively:

<code class="matlab">x = [1, 2, 3];
y = [4, 5, 6];
z = [7, 8, 9];
plot3(x, y, z);</code>

2. Use the mesh function

The mesh function is used to draw a three-dimensional mesh surface. It accepts two matrices as input, representing the x and y coordinates respectively:

<code class="matlab">[X, Y] = meshgrid(linspace(-2, 2, 100));
Z = X.^2 + Y.^2;
mesh(X, Y, Z);</code>

3. Use the surf function

surf Function is similar to the mesh function, but draws a colored surface. It accepts as input three matrices representing x, y and z coordinates, and one matrix representing color:

<code class="matlab">[X, Y] = meshgrid(linspace(-2, 2, 100));
Z = X.^2 + Y.^2;
C = Z;  % 使用 Z 作为颜色
surf(X, Y, Z, C);</code>

4. Using the scatter3 function

scatter3 The function is used to draw a three-dimensional scatter plot. It accepts three vectors as input, representing x, y and z coordinates respectively:

<code class="matlab">x = rand(100, 1);
y = rand(100, 1);
z = rand(100, 1);
scatter3(x, y, z);</code>

5. Using the histogram3 function

The histogram3 function is used to draw a three-dimensional histogram and display the distribution of data:

<code class="matlab">data = randn(1000, 3);
histogram3(data, 'NumBins', 10);</code>

The above is the detailed content of How to draw three-dimensional graphs in matlab. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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