Matlab - two-dimensional drawing (the most detailed, with related examples)

In order to help students prepare for mathematical modeling and learn the use of Matlab, let's talk about the drawing skills in Matlab today! For scientific computing software such as Matlab, plotting is a very important function. In data processing and analysis, good drawing skills can present data more intuitively, enhance data readability and visibility


Table of contents

1. Two-dimensional data graph 

1. Two-dimensional curve 

1.1 Draw a single two-dimensional curve 

1.2 Draw multiple two-dimensional curves

 2. graph plotyy with two ordinate scales

3. Graphics hold on/off

 4. Set the curve style

5.2 Graphic annotation and coordinate control 

Graphic annotation 

coordinate control 

6. Splitting the graphics window

 2. Other two-dimensional graphs

 1. Polar plot

2. Two-dimensional statistical analysis chart 

3. Scatterplot


1. Two-dimensional data graph 

1. Two-dimensional curve 

1.1 Draw a single two-dimensional curve 

The basic calling format of the plot function is: plot(x,y) where x and y are vectors of the same length, which are used to store the x-coordinate and y-coordinate data respectively 

eg: In the interval of 0≤x≤2π, draw a curve: y=xcos(x) 

x=0:0.05:2*pi;
y=cos(x);
plot(x,y);

 that's how it turned out

The simplest calling format of the plot function contains only one input parameter: plot(x)

In this case, when x is a real vector, draw a continuous curve with the subscript of the vector element as the abscissa and the element value as the ordinate, which is actually a line graph

1.2 Draw multiple two-dimensional curves

  • The input parameter of the plot function is in matrix form  

(1) When x is a vector and y is a matrix with the same dimension as x, multiple curves of different colors are drawn. The number of curves is equal to another dimension of the y matrix, and x is taken as the common abscissa of these curves.  

(2) When x and y are matrices of the same dimension, the corresponding column elements of x and y are used as horizontal and vertical coordinates to draw curves respectively, and the number of curves is equal to the number of columns of the matrix 

(3) For the plot function that contains only one input parameter, when the input parameter is a real matrix, the curve of the element value of each column relative to its subscript is drawn by column, and the number of curves is equal to the number of columns of the input parameter matrix

  • The calling format of the plot function with multiple input parameters is: plot(x1,y1,x2,y2,…,xn,yn)

(1) When the input parameters are all vectors, x1 and y1, x2 and y2, ..., xn and yn form a set of vector pairs respectively, and the length of each set of vector pairs can be different. Each vector pair can draw a curve, so that multiple curves can be drawn in the same coordinates.

(2) When the input parameters are in the form of a matrix, the paired x and y are plotted according to the horizontal and vertical coordinates of the corresponding column elements, and the number of curves is equal to the number of columns in the matrix

x=0:0.05:2*pi;
y=cos(x);
yy=sin(x);
plot(x,y,x,yy);

                                      


 2. graph plotyy with two ordinate scales

In MATLAB, if you need to draw two graphs with different ordinate scales, you can use the plotyy plotting function. The calling format is: plotyy(x1,y1,x2,y2)

Where x1, y1 corresponds to one curve, and x2, y2 corresponds to another curve. The scale of the abscissa is the same, there are two ordinates, the left ordinate is used for x1, y1 data pair, and the right ordinate is used for x2, y2 data pair 

x=0:0.05:2*pi;
y=cos(x);
yy=x;
plotyy(x,y,x,yy);

  

3. Graphics hold on/off

The hold on/off command controls whether to keep the original graphics or refresh the original graphics, and the hold command without parameters switches between the two states 

x=0:pi/100:2*pi;
y1=0.2*exp(-0.5*x).*cos(4*pi*x);
plot(x,y1)
hold on
y2=2*exp(-0.5*x).*cos(pi*x);
plot(x,y2);

 

 

 4. Set the curve style

 MATLAB provides several plotting options for determining the line style, color, and data point marker symbol of the plotted curve, which can be used in combination. For example, "b-." indicates a blue dotted line, and "y:d" indicates a yellow dashed line with a diamond marking the data points. When the option is omitted, MATLAB stipulates that the line type should always be a solid line, and the colors will be in order according to the order of the curves.

To set the curve style, you can add drawing options to the plot function, and the calling format is:

plot(x1,y1,option1,x2,y2,option2,...,xn,yn,optionn)

x=0:pi/100:2*pi;
y1=0.2*exp(-0.5*x).*cos(4*pi*x);
plot(x,y1,'b-.')
hold on
y2=2*exp(-0.5*x).*cos(pi*x);
plot(x,y2,'bp');

 

5.2 Graphic annotation and coordinate control 

Graphic annotation 

The calling format of the graphic labeling function is:

  • title (graph name)
  • xlabel (x-axis description)
  • ylabel (y-axis description)
  • text(x,y, graphic description)
  • legend(legend 1, legend 2,...) 

coordinate control 

The calling format of the axis function is: axis([xmin xmax ymin ymax zmin zmax])

The axis function is rich in functions, and the commonly used formats are: axis equal: the vertical and horizontal axes use equal-length scales.

axis square: Generates a square coordinate system (rectangular by default).

axis auto: Use the default setting.

axis off: Cancel the coordinate axis.

axis on: display the coordinate axis 

  • Use the grid command to add grid lines to the coordinates to control the grid on/off command to control whether to draw grid lines or not, and the grid command without parameters switches between the two states.
  • Adding a border to the coordinates is controlled by the box command. The box on/off command controls whether to add a border line or not, and the box command without parameters switches between the two states 

6. Splitting the graphics window

The calling format of the subplot function is: subplot(m,n,p)

This function divides the current graphics window into m×n drawing areas, that is, n in each line, m lines in total, and the area numbers are numbered according to row priority, and the pth area is selected as the current active area. Allows separate drawing of graphics in different coordinate systems in each drawing area 

x1=0:0.05:100;
x2=linspace(0,2*pi,10);
y1=sin(x1);
y2=sin(x2);
subplot(1,2,1); plot(x1,y1); colormap hsv; title('the first');
 subplot(1,2,2); plot(x2,y2); title('the second');


 2. Other two-dimensional graphs

 1. Polar plot

The polar function is used to draw polar coordinates, and its calling format is: polar(theta,rho,options)

Among them, theta is the polar coordinate polar angle, rho is the polar coordinate vector radius, and the content of the option is similar to the plot function 

 eg: Draw a polar coordinate diagram of r=sin(t)cos(t), and mark the data points

x=0:pi/100:2*pi;
y=sin(x).*cos(x);
polar(x,y,'-*');

 

2. Two-dimensional statistical analysis chart 

In MATLAB, there are many two-dimensional statistical analysis graphics, such as bar graphs, ladder graphs, rod graphs, and filled graphs. The functions used are:

bar(x,y,options)

stairs(x,y,option)

stem(x,y,options)

fill(x1,y1,option1,x2,y2,option2,...)  

 

x=0:pi/10:2*pi;
y=2*sin(x);
subplot(2,2,1);bar(x,y,'g');
title('bar(x,y,''g'')');axis([0,7,-2,2]);
subplot(2,2,2);stairs(x,y,'b');
title('stairs(x,y,''b'')');axis([0,7,-2,2]);
subplot(2,2,3);stem(x,y,'k');
title('stem(x,y,''k'')');axis([0,7,-2,2]);
subplot(2,2,4);fill(x,y,'y');
title('fill(x,y,''y'')');axis([0,7,-2,2]);

 

3. Scatterplot

scatter(x,y)

 This MATLAB function creates a scatterplot containing circles at the locations specified by the vectors x and y. This type of graph is also known as a bubble chart

X=randn(1000,1);
Y=randn(1000,1);
scatter(X,Y);
xlabel('x');
ylabel('y');

 


Drawing is not just simply presenting the data, but more importantly, it helps us discover the laws, trends and relationships in the data through graphical display and analysis. This has important implications for us to make accurate decisions and inferences.

In the near future, I will write an article on 3D drawing as soon as possible. I hope you will support me a lot. I am very happy to help you! ! !

Guess you like

Origin blog.csdn.net/qq_74415153/article/details/132619209