Learning matlab (eight)-drawing (two-dimensional)

table of Contents

(1) plot function

(2) Subplot function

(3) Overlay drawing

(4) Other functions

(5) Draw a straight line

(6) Polar coordinate drawing

(7) Logarithmic and semi-logarithmic coordinate system drawing

(8) Double vertical axis coordinates

(9) Window and label

(10) Obtain and mark data points

(11) Other drawings

<1>Function fplot()

<2>Function ezplot()

<3>Function ezploar()

<4>Function ezcontour() and ezcontourf()

<5> Histogram

<6>Pie chart

<7> Histogram

<8>Area chart

<9>Scatter chart

<10> Contour drawing

<11>Error graph

<12>Filling diagram

<13> Matchstick Diagram

<14> Ladder Diagram

<15> Compass Chart

<16> Feather diagram

<17> Vector field diagram

<18> Comet chart

<19>Pseudo-color drawing

(12) Graphic handle


1. Two-dimensional data visualization

An important reason why MATLAB is widely accepted by the control community is because it provides convenient drawing functions. Matlab provides a series of drawing functions. Users do not need to think too much about the details of drawing, and only need to give some basic parameters to get the required graphics. This kind of function is called high-level drawing function. The drawing of two-dimensional graphics is the basis of other drawing operations. This chapter mainly introduces the drawing of two-dimensional graphics, including two-dimensional drawing, graphic annotation, special graphic drawing, and interactive drawing.

(1) plot function

In MATLAB, the most commonly used function for drawing two-dimensional graphics is plot( ). This function is very powerful. It can draw different graphics through different inputs. The calling format of this function is: 1. Function plot(y) 2. Function plot(x, y) 3. Function plot(x, y, s) 4. Function plot(x1, y1,s1, x2, y2, s2,....), for example:

<1>plot(x,y):

>> t=0.01:0.01:2*pi;
>> y=sin(t);
>> plot(y)

>> x=1:7;
>> y=magic(7);
>> plot(x,y)

<2>plot(x,y,s):

s represents the format of drawing the graph line, input doc plot, you can view the corresponding modification mode of the drawing in the doc file.

<3>plot(y):

Here y can be a matrix or a vector, the abscissa is the index number, and the ordinate is the index value;

>> y=[3+2i,4+5i,5+7i,6+8i,7+9i,10+6i];
>> plot(y);

  

>> y=magic(16);
>> plot(y)

(2) Subplot function

In MATLAB, the subplot is drawn using the function subplot( ). This function can draw several graphs in the same graph window, and use different coordinate systems. subplot(m, n, p): This function divides the current graphics window into m*n plot areas, that is, a total of m lines, n each line, the number of the subplot area is numbered from left to right by line first. This function selects the p-th subgraph as the current active area. In each sub-drawing area, it is allowed to draw graphics separately in different coordinate systems.

x=-pi:pi/10:pi;
subplot(2,2,1);
plot(x,sin(x),'r--');
subplot(223);
plot(x,cos(x),'b:*');
subplot(2,2,[2 4]);
plot(x,sin(x)+cos(x),'g-.^');

(3) Overlay drawing

In practical applications, sometimes it is necessary to superimpose and draw new graphics on top of the already drawn graphics. In MATLAB, the function hold is used to start or close the graph hold function. The calling format of this function is: hold on: start the graph hold function, allowing multiple graphs to be drawn on the same axis. hold off: Turn off the graphics hold function, and you cannot draw graphics on the current axis. hold: Switch between the two states hold on and hold off. hold all: realize the hold on function, and make the new drawing function still use the ColorOrder and LineStyleOrder two attributes in the current coordinate system in order.

x=-pi:pi/10:pi;
plot(x,sin(x),'r:>');
hold on;
plot(x,cos(x),'b-<');

In MATLAB, you can set the parameters of the function axis to control the coordinate axis. The calling format of this function is: axis([xmin xmax ymin ymax]): This function is used to define the range of the x-axis and y-axis. axis([xmin xmax ymin ymax zmin zmax]): This function is used to define the range of the coordinate axis, including the x-axis, y-axis and z-axis three-dimensional graphics. axis([xmin xmax ymin ymax zmin zmax cmin cmax]): This function is used to define the range of the three-dimensional coordinate axis and the color information of the graph.

t = 0.01:0.01:pi;
plot(sin(t),cos(t));
axis([-1 1 -2 2]);

(4) Other functions

The axis function is rich in functions, and the commonly used formats are: axis equal: the horizontal axis and the vertical axis adopt equal length scales. axis square: Generate a square coordinate system (the default setting of the system). axis auto: make the coordinate axis range can accommodate all graphics. axis normal: lift any restrictions on the coordinate axis. axis off: cancel all settings of the axis. axis on: restore all settings of the coordinate spoke.

In MATLAB, grid lines are added to the coordinate axis through the function grid(). The calling format of this function is: grid on: to add grid lines to the current coordinate axis. grid off: cancel the grid lines of the current coordinate axis. grid minor: Set the spacing between the grid lines. grid: Without parameters, switch between grid on and grid off. In MATLAB, add a border to the coordinate axis through the function box(). The calling format of this function is: box on: This function adds a border line to the current coordinate axis. box off: This function cancels the border line of the current axis. box: Without parameters, switch between box on and box off.

In MATLAB, the zoom of graphics is achieved through the function zoom. The calling format of this function is: zoom (factor): This function uses the zoom factor as the zoom factor to zoom the coordinate axis. zoom on: Allow zooming on the coordinate axis. zoom off: prohibit zooming of the coordinate axis. zoom out: restore to the original axis settings. zoom reset: reset the current coordinate axis and restore to the initial value. zoom xon: allows the axis to be zoomed, zoom yon: allows the axis to be zoomed. zoom: Switch between zoom on and zoom off.

In MATLAB, the function pan() is used to drag graphics. When dragging the graphics, the mouse changes to the shape of a hand. The calling format of this function is: pan on: this function opens the drag and drop function of the graph. pan xon: This function turns on the dragging function of the graph in the axis direction. pan yon: This function turns on the drag function of the graph in the axis direction. pan off: This function turns off the drag-and-drop function of graphics. pan: This function switches between pan on and pan off.

In MATLAB, the coordinate value of a point on the graph selected by the mouse is displayed through the function datacursormode. The calling format of this function is: datacursormode on: This function turns on the data cursor function of the graph. datacursormode off: This function turns off the data cursor function of the graph. datacursormode: This function switches between datacursormode on and datacursormode off.

(5) Draw a straight line

In MATLAB, a simple line is drawn through the function line(). The calling format of this function is: line(x, y): This function uses the parameters x and y to draw a straight line. If both x and y are vectors, they must have the same length, and draw the line with the corresponding element as the horizontal and vertical coordinates. If x and y are matrices, draw multiple lines with each column of the matrix as coordinates. line(x,y,z): This function draws a line in three-dimensional coordinates, and the parameters x, y and z are the coordinate values ​​in the three-dimensional coordinate system.

(6) Polar coordinate drawing

In MATLAB, the function polar() is used to draw the polar coordinate system. The calling format of this function is: polar(theta, rho): This function uses the radians as theta and the radius as rho to draw in the polar coordinate system, polar coordinates The function under the system is rho=f(theta).polar(theta, rtho, s): This function sets the line type, mark and color of the curve through parameters. The specific setting information is shown in the following table, in MATLAB , The coordinate value in the polar coordinate system can be converted to the coordinate value in the rectangular coordinate system through the function pol2cart(), and the coordinate value in the rectangular coordinate system can be converted into the coordinate value in the polar coordinate system by the function cart2pol().

theta=0:pi/40:4*pi;
rho=sin(theta);
subplot(211)
polar(theta,rho);
[x,y]=pol2cart(theta,rho);
subplot(212);
plot(x,y);

(7) Logarithmic and semi-logarithmic coordinate system drawing

In MATLAB, in addition to drawing with equal scale scale coordinate system, you can also use logarithmic and semi-logarithmic coordinate system for drawing. These functions are introduced below: 1. Function semilogx()2, function semilogy()3, function loglog( );

x=0.1:0.1:8;
y=log10(x);
subplot(121);
plot(x,y);
title('采用函数plot()绘图');
subplot(122);
semilogx(x,y);
title('采用函数semilogx()绘图')

(8) Double vertical axis coordinates

In MATLAB, the function plotyy() is used to draw the double y-axis coordinate system, and the scale of the abscissa is the same. For the two sets of data, the left y-axis and the right y-axis are used, and their coordinate axis ranges are independent, so that the trend of the two sets of data can be well observed in one graph.

x=0.1:0.1:2*pi;
y=sin(x);
z=10.^x;
plotyy(x,y,x,z,'plot','semilogy');


(9) Window and label

The images drawn by MATLAB are displayed in the graphics window, providing a wealth of drawing tools. The annotations of the graphs in MATALB can use drawing tools or functions. Introduce separately below. In MATLAB, the graphics window is composed of title bar, menu bar, tool bar and graphics area. In the heart of the cup-minimize and close buttons. On the right side of the menu bar package are the maximize, minimize and close buttons of the graphics. The menu bar includes: File (file) Edit (edit), View (view) Insert (insert), Tools (tool), Desktop (desktop Window (window) and Help (help). The arrow on the right side of the menu bar can be The window is displayed on the MATLAB desktop. The graphics drawn by drawing tools can be converted into MATLAB programs. By clicking the menu FilelGenerate Code, MATLAB program codes can be generated. xlabel is the name of the x-axis abscissa, and ylabel is the y-axis vertical. Coordinate name.

In MATLAB, graphics can be annotated through functions, or graphics can be annotated directly in the graphics editing mode. The graph editing toolbar is not displayed by default. You can display the graph editing toolbar by clicking the menu View|Plot Edit Toolbar in the graph window. You can insert text, lines and arrows in the graphics by editing the flying tool bar by graphics, and you can also align the annotations.

In MATLAB, you can add a title to the graph through the function title(), or you can insert a title by clicking the menu Insert|Title after drawing. The title of the graphic is located at the item end of the graphic, which is center-aligned by default. The calling format of the function title() is: title('string'): Set the title of the current graphic as a string. title(...,'PropertyName','PropertyValue',...): This function sets the attributes of the title, such as the font used, font size and color, etc.

When making text annotation, you can control the text. Commonly used settings are: lbf: the font used is bold. lit: The font used is italics. Irm: Use standard form. lfontname {fontname}: Set the font of the text. lfontsize {fontsize}: Set the font size. lcolor fcolornamel: Set the color of the font. In addition, you can also use the function texlabel() to convert MATLAB expressions into Tex format strings, which is very convenient to add Greek scripts or mathematical formulas to the graphics.

The legend (Legend) is used to mark the data of different colors and line types in the graph. The legend (Legend) is used to mark the legend ('string1','string2',...) or legend of the data of different colors and line types in the graph. 'string1''string2': Add a legend, and give the description of each figure in sequence. legend('off') or legend off: Clear the legend. legend('toggle') or legend toggle: switch between display and clear. legend('hide') or legend hide: hide the legend. legend('show') or legend show: display the legend. legend('boxoff') or legend boxoff: hide the border of the legend. legend('boxon') or legend boxon: display the border of the legend.

The color bar is used to display the corresponding relationship between the color and the value in the graph, which is convenient for users to understand the graph. It is mainly used in three-dimensional drawing and two-dimensional contour graph. The user can display the color bar by clicking the menu Insert|Colorbar, or add the color bar by the function colorbar(). colorbar: This function judges the graph to add a color bar, which is on the right outside the graph by default. colorbar('Location'): This function sets the position of the color bar to the string Location.

In MATLAB, you can use text boxes to label graphics. Adding a text box can be added by clicking Insert|Textbox after drawing, or by the functions text() and gtext( ). When the function text() adds a text box, you need to set the position of the text box. The calling format is text(x, y,'string'): this function adds a text box at the coordinates (x, y) of the two-dimensional plane Mark, the content of the text is a string. text(...,'PropertyName','PropertyValue'): This function sets the format of the text while adding a text box for labeling.

The function gtext() is an interactive text annotation tool. You can select the location of the annotation text by using the mouse. The calling format of this function is: gtext('string'): This function determines the position of the text box by clicking the mouse. The content of the text is a string. gtext('string1','string2',...): This function marks multiple lines of text and etc. at the position of the mouse click. The strings are separated by commas. gtext('string1';'string2';..): This function adds multiple text boxes with multiple mouse clicks, and the strings are separated by semicolons.
 

(10) Obtain and mark data points

The coordinate value of any point on the two-dimensional plane, the calling format of this function is: (x, y)=ginput(n): This function selects a point with the mouse, and their coordinates are the coordinate value of any point on the two-dimensional plane. The calling format is: [x, y]=ginput(n): This function selects points with the mouse, and their coordinates [x, y]=ginput: This function selects multiple points with the mouse, and the coordinate values ​​are saved in and, End with the Enter key. [x, y, button]=ginput(….): The return value of this function records the relevant information when selecting a point with the mouse.

(11) Other drawings

If you only know the expression of a certain function, you can also draw a graph of the function. Introduce separately below. 1. Function fplot() 2. Function ezplot() 3. Function ezpolar()4, function ezcontour() and ezcontourf().

<1>Function fplot()

The function fplot() is used to draw the graph of the unary function. The figure on the right is drawn by the fplot function. It will adjust the graph spacing adaptively.

x=0:0.01:1;
y=cos(tan(pi*x));
subplot(121);
plot(x,y,'-b');
subplot(122);
fplot('cos(tan(pi*x))',[0 1],1e-4);

<2>Function ezplot()

The function ezplot() is used to draw the graph of a binary function

subplot(121);
ezplot('x.*y+x.^2+y.^2-1');
subplot(122);
ezplot(@cos,@sin,[-2*pi 2*pi]);

<3>Function ezploar()

The function ezploar() is used to draw polar coordinate graphics.

subplot(121);
ezpolar('sin(3*t)',[0 2*pi]);
subplot(122)
ezpolar(@(t) sin(3*t),[0 2*pi]);

<4>Function ezcontour() and ezcontourf()

subplot(121);
ezcontour('x^2-y^2-1');
subplot(122);
ezcontourf(@peaks);

<5> Histogram

b In MATLAB, draw a histogram through the function bar(). The calling format of this function is: bar(y): This function draws a long bar with the length of the value in y. bar(x, y): This function draws y on the specified abscissa x, and the parameter x increases monotonically. If y is a matrix, draw each row vector bar(.., width): This function sets the width of the bar, the default value is 0.8. If the width is greater than 1, the bars in a group overlap each other. bar(..'style'): The parameter style of this function defines the shape, the value can be group or stack. The default value is group.ar(...,'bar_color'): This function defines the color of the bar as bar_color.

<6>Pie chart

In MATLAB, the pie chart is drawn through the function pie(). The calling format of this function is: pie(x): This function draws the pie chart with a vector. If sum(x)<1, draw a pie chart with the value of the vector x for each component. If sum(x)>1, then normalize. pie(x, explode): The parameter explode in this function has the same dimensionality as x, and the component of the vector x corresponding to the non-zero elements protrudes outward in the pie chart. pie(..., labels): This function is used to define the label of each component, which has the same dimension as the vector x.

<7> Histogram

In MATLAB, the histogram is drawn through the function hist(). The calling format of this function is: h=hist(y): This function puts the elements in the vector y into the histogram of 10 bars, and the return value h is containing A vector of the number of elements in each bar. If the parameter y is a matrix, draw the picture according to the columns of the matrix. h=hist(y, m): The parameter m is a scalar, which is used to specify the number of bars. h=hist(y,x): The parameter x in this function is a vector, and the elements in the parameter are placed in length(x) a histogram centered at the position specified by the element in x.

<8>Area chart

In MATLAB, the area graph is drawn through the function area(). The calling format of this function is: area(x, y): This function draws the area graph with the parameters x and y. If x and y are vectors, it is equivalent to the function plot(x, y), and fills between 0 and y. If the parameter y is a matrix, draw an area graph for each column of y and perform a cumulative sum. area(y): If the parameter y is a vector, an area graph is drawn; if y is a matrix, the sum of the area graph of each column is drawn. area(..., level): This function draws the area chart of y=level, the default value of parameter level is 0.

<9>Scatter chart

In MATLAB, the scatter plot is drawn through the function scatter(). The calling format of this function is: scatter(x, y,s, c): This function draws scatter points with x and y as the abscissa and ordinate respectively, The parameter s sets the size of the scattered points, and the parameter c sets the color of the scattered points. scatter(x, y): This function draws scatter points with x and y as the abscissa and ordinate respectively, using the default size and color of the system. scatter(..., m): This function sets the scatter point to m instead of the default "scatter(....'filled'): This function fills the empty scatter points.

In MATLAB, you can also use the function plotmatrix() to draw multiple scatter plots. The calling format of this function is: plotmatrix(X, Y): The parameter X in this function is a matrix of p*m, and the parameter Y is n*p Draw n*m scatter plots, where the (i, j)th scatter plot is drawn based on the data in the i-th column of matrix Y and the j-th column of the moment drop X. plotmatrix(Y): This function is equivalent to plotmatrix(Y, Y), when the parameter Y is a p*n matrix, n*n scatter plots are drawn. The diagonal block of the figure draws a histogram of the frequency of each column of matrix Y. plotmatrix(...,'LineSpec'): This function uses the string LineSpec to set the line type and color of the curve.

<10> Contour drawing

In MATLAB, you can also use the function plotmatrix() to draw multiple scatter plots. The calling format of this function is: plotmatrix(X, Y): The parameter X in this function is a matrix of p*m, and the parameter Y is n*p Draw n*m scatter plots, where the (i, j)th scatter plot is drawn based on the data in the i-th column of matrix Y and the j-th column of the moment drop X. plotmatrix(Y): This function is equivalent to plotmatrix(Y, Y), when the parameter Y is a p*n matrix, n*n scatter plots are drawn. The diagonal block of the figure draws a histogram of the frequency of each column of matrix Y. plotmatrix(...,'LineSpec'): This function uses the string LineSpec to set the line type and color of the curve.

<11>Error graph

In MATLAB, the error graph is drawn through the function errorbar(). The calling format of this function is: errorbar(y, e): the function draws the vector y, and the error of the vector y e.errorbar(x, y,e): the In the function, x, y and e are variables of the same type errorbar(x, y,e, LineSpec): the function uses the parameter LineSpec to set the line type and color.

<12>Filling diagram

In MATLAB, the polygon fill map is drawn through the function fil(). The calling format of this function is: fill(x, y, c): This function draws the fill map of the polygon composed of vectors x and y, and the parameter c is used to set The fixed color can be a string representing the color, such as'r,'g','b','y', etc., or it can be an RGB color vector, such as: [1,0, 0]. If x and y are matrices of the same dimension, draw a polygonal filling map for each column of the matrix. fill(x1, y1, c1, x2, y2, c2,..: This function draws multiple polygon filling maps.

<13> Matchstick Diagram

In MATLAB, the matchstick graph is drawn through the function stem(). The calling format of this function is: stem(y): This function draws the matchstick graph of the vector y. If y is a matrix, draw the matchstick diagram for each column. stem(x, y): This function draws a matchstick graph with the abscissa x. stem(..,'filled'): This function fills the matchstick. stem(...,'LineSpec'): This function is used to set the line type and color of the matchstick graph.

<14> Ladder Diagram

In MATLAB, the ladder diagram is drawn through the function stairs(). The calling format of this function is: stairs(y): This function draws the ladder diagram of the vector y. stairs(x, y): This function draws a ladder diagram with the abscissa x.

<15> Compass Chart

In MATLAB, the compass graph is drawn through the function compass(). The calling format of this function is: compass(u, v) This function draws the compass graph, pointing from the origin of the coordinates to the arrow of the coordinates (u, v). Compass(z): This In the function, z is a complex number, which is equivalent to compass(real(z),imag(z)), compass(...,'LineSpec'): This function uses the parameter LineSpec to set the line type and color.

<16> Feather diagram

In MATLAB, the feather graph is drawn through the function feather( ). The calling format of this function is: feather(u, v): This function draws the feather graph of the vectors u and v. Feather(z): In this function, z is a plural number, Equivalent to feather( real(z),imag(z)).feather(...,'Lifespec'): This function uses the parameter LineSpec to set the line type and color.

<17> Vector field diagram

In MATLAB, the vector field diagram is drawn through the function quiver(). The calling format of this function is: quiver(x, y, u, v): This function draws the vector field diagram at the coordinates (x, y), (u, v) is the velocity component. quiver(u, v): This function draws a vector field map. quiver(...,'LineSpec'): This function uses the parameter LineSpec to set the
line type and color. quiver(....'LineSpec','filled'): This function is set to fill mode.

<18> Comet chart

In MATLAB, the comet diagram is drawn through the function comet(). The calling format of this function is: comet(y): This function draws the comet diagram whose route is determined by the vector y. comet(x,y): This function draws a comet chart whose route is determined by the vectors x and y. comet(x, y, p): This function sets the length of the comet body to p*length(y), and the default value of the parameter p is 0.1.

<19>Pseudo-color drawing

In MATLAB, the pseudo-color drawing is performed through the function pcolor(). The call format of this function is: pcolor(C): This function draws the pseudo-color map of matrix C. pcolor(X, Y, C): This function uses the parameter X to determine the abscissa and the parameter Y to determine the ordinate, and draw a pseudo-color map.

(12) Graphic handle

In MATLAB, use the function set() to set the attribute value of the graphics handle, and use the function get() to get the attribute value of the graphics handle. The calling format of the function set() is: set(H,'PropertyName', PropertyValue), this function sets the handle to the property value of the graphic object, and sets the property to PropertyValue. The calling format of the function get() is: get(H ,'PropertyName'), this function gets the value of the property of the graphics object whose handle is PropertyName.

In MATLAB, the handle of the coordinate axis in the current graph is obtained through the function gca. Through the coordinate axis handle, the function get() can be used to obtain the attribute value of the coordinate axis, and the attribute value of the coordinate axis can also be set by the function set().

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/115298487