Summary of Matlab drawing graphics knowledge points (3)

1. Unconventional coordinate map

1. semilogx (X, Y, S) / semilogy (X, Y, S) single-logarithmic coordinate diagram

        semilogx / semilogy can draw the coordinate diagram of the logarithmic function, and its calling method is similar to the plot() function (refer to the plot function in the previous section for the usage method), and only one direction of the drawn coordinate diagram is logarithmic coordinates form (x/y)

The effect is as follows:

x=0:0.1:5;
y=log10(x);
subplot(2,1,1);        %回忆一下subplot的计数顺序是先行还是先列的
plot(x,y,'-.ko')       %k为黑色,o指的是圆圈标记
subplot(2,1,2);
semilogx(x,y,'--gv')   %g为绿色,v指的是向下三角形

2.contour / clabel surface contour/contour label

        This label is difficult to understand, but the usual frequency of use is also small, a little understanding is enough

        These two functions often appear in pairs when drawing surface contours. clabel needs to use the output of the contour function as input in order to add a label representing the size of the quantity on the contour map. Common calling formats are as follows:

(1)contour(Z)

        Draw a two-dimensional contour map of the Z matrix (Z is the z-coordinate matrix that is arranged in the order of the xy position and processed by the function), the matrix Z is at least 2*2 in size, and it will be returned after the contour function is processed. The matrix of the contour coordinates needs to be designed to receive the code, and the number and value of the contour lines are automatically selected based on the maximum and minimum values ​​of Z.

(2)contour(Z,n)

        When n is a number, the addition of n indicates the specified number of contour lines; when n is a vector, the addition of n controls both the value of the contour line and the number of contour lines.

(3)contour(Z,......,LineSpec)

        The parameter LineSpec indicates the specified line shape, marker symbol and color, and the specific calling method is similar to plot.

Let's try to understand it based on an example, with explanation inside:


z=[1 2 3;4 5 6;7 8 9]   %Z是已经处理好的对应x-y位置的z坐标数值矩阵
[c,h]= contour(z,3);    %将Z代入contour函数,后面的“3”为数字型表示的等高线的条数,
                        %返回的是两个值,第一个值c是关于等高线各点坐标的矩阵,
                        %第二个值h是等高线各项绘画的参数,用于指导如何根据所给的坐标画出圆等高线
clabel(c,h);            %然后将两个参数代入clabel函数
colorbar                %给各等高线添加不同的颜色

        In the above code, the c and h values ​​returned by the contour function are respectively the matrix of the coordinates of each point on the contour line and the parameters of the drawing of the contour line, which are used to guide how to draw a circle contour line according to the given coordinates. Let's explain a little bit why the returned c and h are:

(Compared with the contour line drawn above, the z-coordinate matrix in this example shows that its coordinates are distributed upside down)

         The returned c value is a matrix with two rows and multiple columns. In this example, it is a matrix with two rows and thirteen columns. The matrix can be divided into multiple parts, if the following is divided into three parts. The first row and first column of each part (upper left corner element) is the coordinate value of the contour line to be drawn in this part, and the three parts are "3", "5" and "7" respectively; then the second row and the second The column (the element in the lower left corner) is the number of coordinates required by the contour line of the main words in this part, and the three parts are "3", "4" and "3" respectively; all the following parts except the first column represent the coordinates ( x, y) x is in the first line, y is in the second line, for example, the three coordinate points required for the first part are (1, 1.667) (2, 1.3333) (3, 1), each line is of equal height The number of coordinate points required by the line depends on the calculation of the system, and the more complex the curve drawn, the more coordinate points required.

        The returned h value is a set of attributes, which store various parameters for drawing the contour line, including the contour value, line color, width, style, etc. If you need to modify the style of the contour line, you can Customize in the contour function.

 If you need to draw the contour line as a dotted line: (modify the above sentence as)

[c,h]= contour(z,3, 'linestyle',':');

2. Various 3D drawing functions

1.mesh draws a three-dimensional grid map

(1)mesh(Z)

        If it is known that the Z coordinate corresponds to the numerical solution matrix of the XY coordinate, you can directly use mesh(Z) to draw the image. When [m,n]=size(Z), that is, when the Z matrix is ​​m rows and n columns, the coordinate axis of the image Then it is X=1:n, Y=1:m, and its color is determined by the height.

z=[1 2 3;4 5 6;7 8 9]
mesh(z)
xlabel('X')
ylabel('Y')
zlabel('Z')

The effect is:

 (2)mesh(X,Y,Z)

        If the numerical solution matrix corresponding to the XY coordinates of the Z coordinates is known, a grid map defined by X, Y, and Z is generated, and when X, Y are vectors with lengths of m and n respectively, (n,m)=size( Z ) , you can generate an intersection matrix whose intersection points are [X(i,j),Y(i,j),Z(i,j)] . The difference between it and mesh(Z) is that its coordinate range can be customized.

 For example:

z=[1 2 3;4 5 6;7 8 9];
x=[4:6];
y=[7:9];
mesh(x,y,z)
xlabel('X')
ylabel('Y')
zlabel('Z')

The effect is:

(3)meshgrid()

        Create a one/two/three-dimensional grid coordinate matrix using its vector coordinates according to the number of parameters. It is convenient to provide a plane when drawing in 3D, and it can be applied to the premise of solving the matrix numerically with unknown Z coordinates. You can directly use the relationship between Z, X, and Y to calculate the 3D graphics. The specific usage method is as follows:

        (i)[X,Y]=meshgrid(x,y)

                Returns a 2D grid coordinate network based on the coordinates contained in the vectors x and y

        (ii)[X,Y]=meshgrid(x)

                Similar to (i), the returned grid size is the square grid coordinates of length(x)*length(y)

        (iii)[X,Y,Z]=meshgrid(x,y,z)

                Similar to (i), the returned grid size is length(y)*length(x)*length(z)

Here is a concrete example:

[X,Y]=meshgrid(-8:.5:8);
R=sqrt(X.^2+Y.^2);
Z=sin(R)./R;
mesh(X,Y,Z);
xlabel('X')
ylabel('Y')
zlabel('Z')

        In the above code, first set up an XY coordinate matrix, and then list its relationship formula according to the relationship between Z and X and Y (an intermediate variable R is set in the middle). Note that the matrix needs to be processed for each element value inside Using dot operations (.*dot multiplication./dot division.^ dot power), a three-dimensional Z=sinc(x,y) function is drawn according to its relationship.

         The above method is very suitable for finding the three-dimensional graphics of the known X, Y, Z relational expressions, and readers can practice more to master the skills.

2.surf function

        The surf function is used to draw a smooth three-dimensional surface. The drawn surface is created by interpolation among the given data points. However, due to its difference processing characteristics, the drawing speed is not as good as mesh when the amount of data is huge. function; similarly, it also has its own advantages. The surface map drawn by the surf function can use color and light to represent the height change of the surface, while the mesh function cannot.

        Its calling method is as follows:

 [X,Y]=meshgrid();

surf(X,Y,Z);

Its calling method is the same as the mesh function, as far as the above example is concerned:

[X,Y]=meshgrid(-8:.5:8);
R=sqrt(X.^2+Y.^2);
Z=sin(R)./R;
surf(X,Y,Z);
xlabel('X')
ylabel('Y')
zlabel('Z')

Every plane is a smooth plane

        

        A simple summary of the mesh function and the surf function: the surf function is suitable for situations where smooth surfaces need to be visualized, and the mesh function is suitable for situations where the grid structure and shape of the data need to be displayed. Which function to choose depends on your needs and the characteristics of the data to be expressed.

3.plot3 function

        The plot3 function is used to draw continuous curves or scatter plots in three-dimensional space. The effect it achieves is the moving trend graph of the curve in three-dimensional space. The specific calling method is:

plot3(X,Y,Z,LineSpec)

        Among them, X, Y, and Z are vectors or matrices containing the coordinates of data points, and LineSpec is a style parameter. A specific example is given below to understand how to use it:

% 创建数据
t = linspace(0, 2*pi, 100);
x = cos(2*t);
y = sin(2*t);
z = t;

% 绘制三维曲线
plot3(x, y, z, 'b-', 'LineWidth', 2);

% 设置坐标轴标签和标题
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Curve Plot');

% 设置其他参数(可选)
grid on;      % 显示网格
axis equal;   % 设置坐标轴刻度相等

Its realization effect is:

3. Sharing summary

        The above is a summary of all the matlab drawing knowledge points shared with you. I hope you will gain something after reading it. If you have any mistakes or supplements, you can contact me. If you have any questions, you can leave a message in the comment area, or chat with me privately. I will Do what you can to help those who study with you. Let's make progress together! This part of the knowledge point has come to an end, thank you everyone!

Guess you like

Origin blog.csdn.net/FSHznb/article/details/131587780