Summary of knowledge points for drawing graphics in Matlab (1)

        In actual operation, we can easily command Matlab to draw graphics through code design. The following is a summary of the most commonly used drawing functions of Matlab (including two-dimensional and three-dimensional graphics).

1. Two-dimensional image drawing function plot()

        1.plot(X,Y);       

        If and only when X and Y are real vectors and have the same dimension (row vectors of the same length or column vectors of the same length), Matlab will correspond to their abscissa and ordinate one by one in their order, namely [X (i), Y(i)]. If both X or Y are complex variables, the imaginary part will be ignored.

X=[1 2 3 4 5 6 7 8];    %均为行向量或者均为列向量
Y=[8 7 6 5 4 3 2 1];
plot(X,Y);
X=[1 2 3 4 5 6 7 8+6i];    %Matlab画图时提示警告自动忽略虚数部分
Y=[8 7 6 5 4 3 2 1+6i];
plot(X,Y);

(The graphics drawn by the above two are consistent)

        If one of X and Y is a vector and the other is a matrix, and the length of the vector is equal to the number of rows or columns of the matrix, the function will decompose the matrix into multiple vectors by row or column, which correspond to X\Y one by one, respectively in the image Draw its corresponding graph.

X=[1 2 3 4 5 6 7 8];    %X为向量
Y=[8 7 6 5 4 3 2 1;7 6 5 4 3 2 1 0; 6 5 4 3 2 1 0 -1];   %Y为三行八列的矩阵
plot(X,Y);

 (The effect drawn is as above)

       

         2.plot(X1,Y1, X2,Y2, .....,Xn,Yn)

        Matlab supports multiple functions to draw pictures at the same time, and display them on the same canvas, and the effects will not be described in detail.

        

        3.plot(X,Y,S)

In Matlab, you can customize its image style, and you can switch between various line and coordinate styles according to your own style or the requirements of the paper. Some commonly used definers are summarized below. (Note that the abbreviation for black is not b but k)

        (1) Line shape and color definers

Line style and color symbol table

definer - -- -.
Linear Solid line (Matlab default) draw a line Dotted line Dotted line
definer r g b c
color red green blue Cyan (cyan)
definer m y k w
color Magenta (magenta) yellow black white

        (2) Token type definer

Tag Type Definer Table

definer + o * . x
mark type plus small circle Asterisk real point cross number
definer d ^ v > <
mark type diamond upward triangle downward triangle right triangle left triangle
definer s h p
mark type square Hexagon Positive five-pointed star (pentagon)

        (3) Specify the mark size and color

MarkerSize Marker size, specified as a positive value
MarkerEdgeColor Marker outline color, specified as a color name or an RGB triplet
MarkerFaceColor Marker inner color, specified as a color name or an RGB triplet

For example like the following example:

X=-pi:pi/10:pi;
Y= sin(X);
plot(X,Y,'--mv','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor',[0.4 0.6 0.7])
%'--mv'表示图像用划线表示,品红色,所在具体值用向下三角形表示
%后面的MarkerSize、MarkerEdgeColor、MarkerFaceColor分别表示标记符的大小、边框颜色、内容颜色

The effect is as follows: (NoteMarkerFaceColor若用RGB三元组表示的话各元素需要用空格隔开且可不用‘’(引号)包裹,但是若用颜色名称表示的话则需要‘’包裹,如‘g’)

        This part requires readers to practice and observe the phenomenon, and you can try the specific effect of the icon above.

2. Coordinate axis setting function

        Plotting in Matlab is often related to coordinate axes. The following summarizes the commonly used coordinate axis functions.

Axis setting function

function Function
axis Set the interval of the horizontal and vertical axes
xlable/ylable Add labels for horizontal and vertical axes
legend Add a legend to the graph in the axes
text Add strings to axes
grid add grid
box add border
xlim Set the horizontal and vertical coordinate interval

        1.axis can be used to set the interval of the horizontal and vertical axes, adding different codes can achieve various requirements. (You don't need to add ';' to this line)

           (1)       axis([xmin,xmax,ymin,ymax])

        Control the interval of the x/y axis in the canvas, and add this line of code on the basis of the code of the previous knowledge point, the effect is:

(Whether the required interval has a value or not, the canvas will be drawn)

axis([-5,10,-2,2])

         (2)      axis equal

        The scale value of the horizontal coordinate and vertical coordinate of the displayed image of the control canvas is equal.

 (The scale values ​​of the horizontal and vertical coordinates are all 1)

         (3)      axis square

        The horizontal and vertical coordinates of the image displayed on the control canvas are equal in length. (The shape of the canvas is square at this time)

         (4)axis auto

        The system default style (default value), it is possible to fill the canvas.

        2. The xlim/ylim function can set or return an interval function

         (1) xlim

        Returns the interval of the abscissa, and returns a two-dimensional row vector [min max]

 

The same is true for the ordinate direction.

         (2) xlim([xmin,xmax]) (ylim is the same)

        The canvas display interval used to set the horizontal and vertical coordinates is similar to the method of axis([-5,10,-2,2]) above, so I won’t go into details here

  

      3. The legend function

        Add icon text to graphics in the canvas.

        (1) legend('str1 first string', 'str2 second string', 'str3 third string'...)

        It will mark each string near each image in the drawing order before the code, so the order is very important. Add a knowledge point here: when you need to draw two or more curves in one canvas, you need to add "hold on;" after each drawing to keep the next drawing on the canvas.

X=-pi:pi/10:pi;
Y1= sin(X);
Y2= cos(X);
plot(X,Y1,'--mv','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor',[0.4 0.6 0.7])
hold on;
plot(X,Y2,'--m^','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor',[0.4 0.6 0.7])
hold on;
legend('sinx','cosx')

        For example, in the above code, the first character string inside the legend will match the first curve, which is X-Y1, and the following is similar. The result of the operation is as follows:

(If the icon is not modified, it will be in the upper right corner by default)

         (2) When the icon position needs to be modified according to the curve image

        legend('str1 first string', 'str2 second string', 'str3 third string'..., 'Location', 'direction')

        Sometimes when the icon needs to modify the position because the icon blocks the image at the default position, you can add the location attribute after it.

        its direction has

northwest north northeast
west east
southwest south southeast

        In the above example, change the legend function to "legend('sinx', 'cosx', 'Location', 'northwest')", which can make the function distribution more regular and beautiful.

        

         4.text function text(x,y,'string')

        The text function can add a string at the specified position of the coordinate axis for more direct interpretation of the image. Its coordinate position (x, y) is related to the distribution of horizontal and vertical coordinates.

        Taking the above image as an example, if you want to add the string "y=sinx" at the coordinate axis (-3, 0.3) to explain the graph line, you can add the text function to the original code, that is

X=-pi:pi/10:pi;
Y1= sin(X);
Y2= cos(X);
plot(X,Y1,'--mv','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor',[0.4 0.6 0.7])
hold on;
plot(X,Y2,'--m^','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor',[0.4 0.6 0.7])
hold on;
legend('sinx','cosx','Location','northwest')
text(-3,0.3,'y=sinx')

The effect is as follows:

 5.label function xlabel/ylabel

        The label function is used to add labels to the horizontal and vertical coordinates, and the calling form is xlabel('str1') or ylabel('str2') . The implementation is relatively simple but not too much description.

Three, sub-paste summary

        Since there are too many knowledge points, one post will take too long to share, so I plan to write one more post to share the knowledge points about drawing later. If you need to continue learning, you can find the following posts to study. If you have any questions, you can leave a message in the comment area, or chat with me privately, and I will do my best to help those who study together. Let's make progress together!

Guess you like

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