MATLAB 2D plot
1.plot two-dimensional line drawing function
a. plot(y):
b. plot(x,y):If y is a vector, the y value is the ordinate, and the element number is the abscissa;
x=0:pi/100:2*pi; y=2*exp(-0.5*x).*cos(4*pi*x); figure plot(y)Then the matlab plotting result is as follows, it can be seen that although y is a function of x, the abscissa in the figure takes 0 to 200.
figure 1
If y is a real matrix, draw each curve by column, each element of each column is the ordinate, and the number of rows is the abscissa;
y=rand(8,6) figure plot(y)The generated 8*6 random matrix is
Then the matlab drawing is as follows, there are six different curves in the figure, and the abscissa takes 8 points from 1 to 8.
figure 2
If both x and y are vectors, their lengths must be the same, and the graph is drawn with x as the abscissa and y as the ordinate;
x=0:pi/100:2*pi; y=2*exp(-0.5*x).*cos(4*pi*x); figure plot(x,y)The matlab plot is as follows. It can be seen that the difference from Figure 1 is only the horizontal axis range. At this time, the horizontal axis is 0~2π.
image 3
If x is a vector and y is a matrix, when the number of y rows is the same as the length of the x vector, each curve is drawn by column; when the number of y columns is the same as the length of the x vector, each curve is drawn by row; when y is a square matrix and When the number of rows and columns is the same as the length of x, draw each curve by column; where x is the abscissa;
x=0:pi/100:2*pi; y=[]; for i=1:1:5 yi = sin (x + i); y=[y;yi]; end figure plot(x,y)The matlab plot is as follows. The 5*201 matrix y generated in the code is drawn with each line.
Figure 4
x=0:pi/100:2*pi; y=[]; for i=1:1:6 yi = sin (x + i); y=[y yi']; end figure plot(x,y)The matlab drawing is as follows, the 201*6 matrix y generated in the code is drawn with each column of the curve.
Figure 5
x=0:pi/100:2*pi; y=[]; for i=0:200 yi = i * sin (x); y=[y yi']; end figure plot(x,y)The matlab plot is as follows. The code generates a 201*201 matrix y. It can be seen that matlab draws a curve in each column, and the amplitude of the curve in each column is different.
Image 6
If both x and y are matrices, the two matrices must be of the same type, draw a curve with each column of y, and use each column of x as the abscissa coordinate;
A=rand(3,4); B=rand(3,4); figure plot(A,B)The generated two 3*4 random matrix values are
According to the corresponding A matrix column element as the abscissa, the B matrix column element as the ordinate, the matlab drawing is as follows, there are 4 different curves in the figure.
Figure 7
c. plot(x,y,'linespec'):
linespec can be used to specify the color, type and mark type of the line, etc. It is defined by a string and can be required in any order of composition;
line type colour token type - Solid line (default) y yellow . point : Dotted line m purple o round
-. Dotted line c cyan x fork -- dotted line r red + cross g green * star b blue s square w white d rhombus k black ^ Improved triangle v downward triangle < left triangle > Right triangle p pentagon h hexagon
x=linspace(0,2*pi); y=sin(x); figure plot (x, y, 'ko')Then the matlab drawing is as follows, where the line color is black and the mark type is circle.
Figure 8
d. plot (x1, y1, x2, y2, ..., xn, at) :
To draw multiple curves, as long as xi and yi have the same length, xi and xj, yi and yj can have different lengths; similar to plot(x1, y1,'linespec',x2,y2, 'linespec' ,. ..,xn,yn, 'linespec' );
x1=-pi:pi/20:pi; x2=0:pi/10:pi; x3=0:pi/10:2*pi; y1=sin(x1); y2 = cos (x2); y3=sin(x3-pi/2); plot(x1,y1,'k',x2,y2,'r',x3,y3,'b');The matlab plot is as follows.
Figure 9
e. plot(_,name,value):
Using the above format, the width of the curve, the size of the mark and the color of the mark can be defined; the outline color of the mark is defined by a string, and the fill color of the mark is defined by the RGB value;
Use the help example in matlab to illustrate:
x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); figure plot(x,y,'--gs',... % Set the line type to dashed line, color green, and mark type to be square 'Linewidth',2,... % set the line width 'MarkerSize',10,... % Set the marker size 'MarkerEdgeColor','b',... % Set the marker edge color to blue 'markerfacecolor',[0.9 0.5 0.8]) % set marker fill colorThe matlab drawing is as follows.
Figure 10
2. Special 2D plotting functions
Currently less used temporarily listed first.
function Drawing function area draw an area chart bar、barh
Draw bar graph, horizontal bar graph compass Plot Azimuth Complex Numbers feather draw feather diagram hist Draw a bar chart rose Plot angle histogram errorbar Plot the error histogram pie draw a pie chart polar Plot polar coordinates ribbon Draw a flaky ribbon diagram stairs draw a ladder diagram stem Plot discrete series data