I. Basic Introduction
Basic functions of 2D high-level plotting:
Secondary operations for two-dimensional plotting:
- Annotation (Graphic name, coordinate axis name, curve annotation, legend)
- Coordinate axis persistence
- Graph persistence (drawing multiple images at the same coordinate)
The following describes how to use the plot function.
Basic usage: plot (x, y );
Requirement: X and Y are vectors of the same length.
Special usage:
(1) X is a vector, and Y is a matrix. If one dimension is the same as x length, multiple curves of different colors are drawn. (How can we determine the color ?);
(2) X and Y are the same-dimension matrices, and different color curves are drawn based on corresponding columns;
(3) plot has a parameter. If X is a real number vector, a line chart is drawn. If X is a complex number vector, the real and virtual parts are used as the horizontal and vertical coordinates;
(4) multiple input parameters;
(5) curve options
Next, let's take a look at the specific usage.
Let's first draw a sine function, draw 0 ~ Content between 2 π.
x = 0:0.01:2*pi;y = sin(x);plot(x,y);
Run the following sine function:
Of course, you can also draw two straight lines at the same time and draw multiple curves with different colors:
x = 0:0.01:2*pi;y = sin(x);y1 = sin(x);y2 = cos(x);z = [y1 ; y2];plot(x,z);
The drawing effect is as follows:
For example, set multiple row vectors for Z:
x = 0:0.01:2*pi;y = sin(x);y1 = sin(x);y2 = cos(x);y3 = exp(x);y4 = x;y5 = tan(x);z = [y1;y2;y3;y4;y5];plot(x,z);
Multiple colors are still used for drawing:
Of course, you can also get a few more to make cool Interface Effects:
We need to transpose the matrix and draw two curves:
% Both parameters are matrix X1 = 0: 0. 0* PI; x2 =-Pi: 0.01: PI; X = [x1; x2] '; % transpose Y1 = cos (X1); y2 = sin (X2 ); y = [Y1; y2] '; % transpose plot (x, y)
The drawing is as follows:
Next, let's take a look at the condition that the plot only has one parameter.
We can use linespace to generate row vectors:
% Plot only has one parameter x = linspace (0, 2 * Pi, 200); y = sin (x); plot (y) y2 = cos (X ); y3 = Y + I * Y2; plot (Y3) axis equal
If there is only one parameter, each point of Y is used as the abscissa during plotting:
Let's take a look at the drawing of the plural:
x = linspace(0, 2*pi, 200);y = sin(x);y2 = cos(x);y3 = y + i*y2;plot(y3)axis equal
The drawn image draws the x-axis based on the actual part of Y and the y-axis based on the Y-axis. The axisequal is used to control the x-axis:
The following figure shows that the plot has multiple parameters.
x1 = linspace(0, 2*pi, 200);x2 = linspace(0, 2*pi, 100);y1 = cos(x1);y2 = sin(x2);plot(x1, y1, x2, y2)
Y1 is the cosine of X1 and Y2 is the sine of X2. We can use the multi-parameter function of plot to plot it:
Then there is a question about the plot parameter.
% Plot curve options X = linspace (0, 2 * Pi, 100); y = sin (x); plot (X, Y, 'k ') % r g y m k B, change the color of the curve to plot (X, Y, 'P') % *. P <> use the star, vertex, pentagram, and triangle signs to mark plot (X, Y, ':') % ----.: dotted line or solid line plot (X, Y, 'r *: ') % can also be used in combination
2. Graphic annotation.
Annotation indicates the name of the horizontal and vertical coordinates, for example, the following code:
X = linspace (0, 2 * Pi, 100); y = sin (x); plot (x, y) xlabel ('x') ylabel ('y ') title ('sine ');
The execution result is as follows:
There are many other annotations, such as text annotation:
X = linspace (0, 2 * Pi, 100); y = sin (x); plot (x, y) xlabel ('x') ylabel ('y ') title ('sine '); text (2, 0.3, 'y = Xin (x)'); text (5, 0.5, 'x _ 2 ')
The effect is as follows:
Another Annotation is the legend:
x1 = linspace(0, 2*pi, 200);x2 = linspace(0, 2*pi, 100);y1 = cos(x1);y2 = sin(x2);plot(x1, y1, x2, y2)legend('cos', 'sin');
The effect is that the legend is displayed in the upper right corner. The coordinates can be changed using the underlying drawing:
When drawing two lines, the first curve disappears. This is because the image persistence is not enabled. You can use hold on to enable it and hold off to disable it:
% Image hold x = 0: 0. * PI; Y1 = sin (x); y2 = cos (x); hold onplot (x, Y1, 'R'); plot (x, Y2, 'G'); hold off
Next let's take a look at the window segmentation. For example, to draw a graph in the four corners of the window, you can use the subplot function:
% Window split x = 0: 0. * PI; Y1 = sin (x); y2 = cos (x); Y3 = tan (x); Y4 = exp (x); subplot (2, 2, 1) % 2*2 cell, drawing the first cell plot (x, Y1); subplot (222) % can be separated, or can be connected to write plot (x, Y2) subplot (223) plot (x, Y3) subplot (224) plot (x, Y4)