Other forms of two-dimensional graphics in MATLAB

Article directory

  • 1. Drawing function for function adaptive sampling
  • 2. Two-dimensional graphs in other coordinate systems
    • 1. Logarithmic coordinate function
    • 2. Polar plot
  • 3. Other special two-dimensional images
    • 1. Bar graph
    • 2. Area graphics
    • 3. Scatter graphics
    • 4. Vector graphics
  • In addition to the rectangular coordinate system, the two-dimensional graph line can also use logarithmic coordinates or polar coordinates. In addition to drawing two-dimensional curves, MATLAB’s two-dimensional graphics also include various two-dimensional statistical analysis graphics.

1. Drawing function for adaptive sampling of functions

  • In the last blog post, we introduced the plot function.
  • The basic operation method is to first take a sufficiently dense independent variable vector

    x

    x

    x, then computes the vector of function values

    the y

    the y

    y, and finally plot with plot function. When taking data points, it is generally equally spaced sampling, which is not accurate enough for drawing functions with high frequency changes. For example function

    f

    (

    x

    )

    =

    c

    o

    the s

    (

    t

    a

    no

    (

    π

    x

    )

    )

    f(x)=cos(tan(\pi x))

    f(x)=cos(tan(πx)), there are infinitely many oscillation periods in the range (0, 1), and the rate of change of the function is large.

  • In order to improve the accuracy and draw a more realistic function curve, it is not possible to sample at equal intervals, but must be intensively sampled in the section with a large change rate to fully reflect the actual change law of the function, thereby improving the authenticity of the graph.
  • The fplot function can adaptively sample the function, which can better reflect the changing law of the function. Its calling format is as follows:
 fplot(filename,lims,options)
  • Among them, filename represents a function, usually in the form of a function handle, or in the form of a string, which is used in many MATLAB functions. Multiple component functions can be specified, in which case they are expressed as unit vectors. lims is the value range of the x-axis, which takes a binary vector [xmin, xmax], and the default value is [-5,5]. The option definitions are the same as for the plot function, for example:
f=@sin; % Use the function handle to specify the function
fplot(f, [0,2*pi],'*') % draw a sine curve
fplot('sin(1./x)',[0.01,0.1]) %Use strings to specify functions, but future versions will not support
fplot({<!-- -->@(x)sin(x),@(x)cos(x)},[0,2*pi],'r.') % use anonymous function to specify function
  • Observing the distribution of the sampling points of the sine and cosine curves drawn by the above statement, it can be found that the sampling points are relatively dense in the section of the curve with a large rate of change.
  • For example, we use the fplot function to plot

    f

    (

    x

    )

    =

    c

    o

    the s

    (

    t

    a

    no

    (

    π

    x

    )

    )

    f(x)=cos(tan(\pi x))

    The curve of f(x)=cos(tan(πx)).

  • The command is as follows:
fplot(@(x)cos(tan(pi*x)),[0,1])
  • The program running result is shown in the figure below.

2. Two-dimensional graphs in other coordinate systems

1. Logarithmic coordinate function

  • In engineering applications, logarithmic coordinates are often used. For example, Bode diagrams in automatic control theory use logarithmic coordinates. MATLAB provides functions for drawing semi-logarithmic and full-logarithmic coordinate curves, and the calling format is as follows:
 semilogx(x1, y1, option 1, x2, y2, option 2,...)
    semilogy(xl,y1,option1,x2,y2,option2,…)
    loglog(xl,y1,option1,x2,y2,option2,…)
  • Among them, the definition of the options is exactly the same as that of the plot function, the difference is the selection of the coordinate axes.
  • The semilogx function uses semilogarithmic coordinates,

    x

    x

    The x-axis is on a common logarithmic scale, while

    the y

    the y

    The y-axis remains on a linear scale.

  • The semilogy function also uses semi-logarithmic coordinates,

    the y

    the y

    The y-axis is on a common logarithmic scale, while

    x

    x

    The x-axis remains on a linear scale.

  • The loglog function uses full logarithmic coordinates,

    x

    x

    x-axis and

    the y

    the y

    The y-axes are all on a common logarithmic scale.

  • For example, we draw

    the y

    =

    10

    x

    2

    y=10x^{2}

    A logarithmic plot of y=10×2 and compare to a Cartesian linear plot.

  • The procedure is as follows:
x=0:0.1:10;
y=10*x.*x;

subplot(2,2,1);
plot(x,y); % Cartesian coordinate curve
title('plot(x,y)');
grid on;

subplot(2,2,2);
semilogx(x,y); %x semi-logarithmic coordinate curve
title('semilogx(x,y)');
grid on;

subplot(2,2,3);
semilogy(x,y); %y semilogarithmic coordinate curve
title('semilogy(x,y)');
grid on;

subplot(2,2,4);
loglog(x,y); % full logarithmic coordinate curve
title('loglog(x,y)');
grid on;
  • The program running results are as follows.

2. Polar plot

  • The polar function draws a polar coordinate graph in meters, and its calling format is as follows:
 polar(theta, rho, options)
  • Among them, theta is the polar angle in polar coordinates, rho is the polar diameter in polar coordinates, and the content of the options is similar to that of the plot function.
  • For example, the butterfly curve is a plane curve full of aesthetic feeling, and its polar coordinate equation is as follows:

    ρ

    =

    e

    cos

    ?

    θ

    ?

    2

    cos

    ?

    4

    θ

    +

    sin

    ?

    5

    θ

    12

    \rho =e^{\cos \theta } -2\cos 4\theta + \sin ^{5} \frac{\theta}{12}

    ρ=ecosθ?2cos4θ + sin512θ?

  • (1) Draw a butterfly curve.
  • (2) adjustment

    θ

    \theta

    The size of θ can change the shape of the curve and its direction, and the

    θ

    \theta

    theta minus

    π

    2

    \frac{\pi}{2}

    2π?, rotate the graph 90°, and draw a butterfly curve.

  • The procedure is as follows:
t=0:pi/50:20*pi;
r1=exp(cos(t))-2*cos(4*t) + sin(t/12).^5;
r2=exp(cos(t-pi/2))-2*cos(4*(t-pi/2)) + sin((t-pi/2)/12).^5;
subplot(1,2,1)
polarplot(t,r1) % draw butterfly curve
subplot(1,2,2)
polarplot(t,r2) %rotate 90°butterfly curve
  • The program running result is shown in the figure below.

3. Other special 2D images

  • In addition to two-dimensional curves, MATLAB can also draw other special two-dimensional graphics, including bar charts, histograms, pie charts, scatter plots, etc. for statistical analysis.

1. Bar graph

  • Bar graphs use a series of stripes of different heights to represent the size of the data, commonly used are bar graphs and histograms. The bar graph is used to display the data size at different time points or to compare the size of each group of data, and the histogram is used to represent the data distribution.
  • (1) Bar chart. The functions for drawing two-dimensional bar graphs are bar (vertical bar graph) and barh (horizontal bar graph), and their calling formats are the same. Taking the bar function as an example, the commonly used calling format is as follows:
  • bar(y): If y is a vector, the height of each component is displayed separately, and the abscissa is the subscript of y. If y is a matrix, then compare the size of elements in each row of y, and the abscissa is the number of rows of the matrix.
  • bar(x,y,style): Draw y at the specified abscissa x. when y is

    m

    x

    no

    m×n

    For an m×n matrix, each row element in the matrix is drawn in a group, and each column element is drawn at the corresponding x position in each group. style specifies the arrangement mode of the bars, the types are grouped (clustered grouping) and stacked (stacked grouping), and the grouped arrangement mode is adopted by default.

  • Application example of bar chart.
  • The procedure is as follows:
x=-1:1;
y=[1,2,3,4,5;1,2,1,2,1;5,4,3,2,1];
subplot(1,2,1);
bar(x,y,'grouped');
title('Group');
axis([-3,3,0,6]);
subplot(1,2,2);
barh(x,y,'stacked');
title('Stack');
  • The program running result is shown in the figure below.

  • (2) Histogram. In MATLAB, there are two functions for drawing histograms: the hist function and the rose function, which are used to draw the histogram in the Cartesian coordinate system and the polar coordinate system respectively. A histogram, also known as a rosette.
  • The hist function is more widely used, and its calling format is as follows:
 hist(y[,x])
  • If y is a vector, divide the numerical interval between the minimum and maximum values in y equally, count the number of vector elements in each interval, and then draw a bar chart with the height of the number of elements.
  • If y is a matrix, the hist function takes each column of x as a vector and draws a histogram of the elements in each column.
  • The option x is used to set the division method of the statistical interval. If x is a scalar, the statistical interval is divided into x small intervals; if x is a vector, the number of intervals is the length of the vector, and each number in the vector specifies the center of each interval Points, when x is omitted, count according to 10 equal intervals.
  • For example, we draw a histogram that obeys the Gaussian distribution, then divide these data into intervals of the specified range, and draw them in the histogram.
  • The procedure is as follows:
y=randn(500,1);
subplot(1,2,1);
histogram(y);
title('Gaussian distribution histogram');
x=-4:0.1:4;
subplot(1,2,2);
histogram(y,x);
title('Gaussian distribution histogram of the specified range');
  • The program running result is shown in the figure below.

  • The calling format of the rose function is very similar to that of the hist function, and the calling method is as follows:
 rose (theta[,x])
  • Among them, the vector theta is used to determine the angle between each interval and the origin (the angle is in radians), and the length of each interval reflects the number of theta elements falling into the interval.
  • If x is a scalar, draw x equidistant small sectors in the interval [0, 2n], the default value is 20. If x is a vector, then x specifies the group center value, and the number of elements in x is the number of data groups.
  • For example, we draw the histogram of the above Gaussian distribution data in polar coordinates.
  • The procedure is as follows:
y=randn(500,1);
theta=y*pi;
polarhistogram(theta);
title('Histogram in polar coordinates');
  • The program running result is shown in the figure below.

2. Area graphics

  • (1) Fan-shaped statistical chart. Pie charts, also known as pie charts, reflect the proportion of each component in a data series to the total quantity. MATLAB provides the pie function to draw pie charts, and its calling format is as follows:
 pie(x,explode)
  • The pie function draws a pie chart using the data in x, which can be a vector or a matrix. If x is a vector, then each element of x occupies a slice, starting at the top of the pie chart, in counterclockwise order for each element of x. If x is a matrix, then array the matrix elements sequentially in column order.
  • If the sum of all elements of x is less than 1, the graph drawn is not a complete circle. explode is a vector or matrix of the same size as x, and the portion corresponding to a nonzero value of explode will be separated from the center of the pie chart. When we omit explode, the pie chart is a whole.
  • For example, the number of excellent, good, medium, passing, and failing in an exam is 7, 17, 23, 19, and 5 respectively. We use a fan-shaped statistical chart for score statistics analyze.
  • The procedure is as follows:
pie([7,17,23,19,5],[0,0,0,0,1]); % corresponds to the fifth component part separated from the center of the pie chart
title('pie chart');
legend('excellent','good','medium','pass','fail');
  • The program running result is shown in the figure below.

  • (2) Area chart. The area chart reflects the trend of quantity changes, and in practice can show the influence of different parts on the whole. In MATLAB, the function to draw an area statistical graph is area, and its calling format is as follows.
  • area(x): Same as the plot(x) function, but fill the area under the obtained curve with color.
  • area(x,y): If both x and y are vectors, it is the same as plot(x,y), but the area under the resulting curve is filled with color. If x is a vector and y is a matrix, then the first column of matrix y is plotted against vector x, and then the sum of the values of the next column and all preceding columns are plotted against vector x, with each region having its own color.
  • For example, let’s draw an area chart.
  • The procedure is as follows:
x=1:2:9;
y=[1,3,5,2,6;2,4,5,6,2;5,4,7,2,2]';
area(x,y);
grid on;
title('area statistics map');
  • The program running result is shown in the figure below.

  • Here, we briefly analyze this picture.
  • First, the elements of x are vectors of [1, 3, 5, 7, 9], and y is the transpose of a matrix with three rows and five columns, so y is five rows and three columns.
  • Then, the vector x corresponds to the three columns of y, that is, when x is 1, the values of y are 1, 2, and 5; when x is 3, the values of y are 3, 4, and 4; When x is 5, the values of y are 5, 5, 7; when x is 7, the values of y are 2, 6, 2; when x is 10, the values of y are 6, 2 ,2.
  • It should be noted here that they need to be accumulated sequentially.
  • (3) Solid figure. The solid map connects the start and end points of the data into a polygon and fills it with color. The function to draw a solid graph is fill, and its calling format is as follows:
 fill(x,y,color)
  • The fill function connects the data points defined by the corresponding elements of x and y with straight line segments according to the increasing order of the subscripts of the vector elements. If the polyline obtained by such connection is not closed, then MATLAB will automatically connect the beginning and end of the polyline to form a closed polygon, and then fill the interior of the polygon with the specified color.
  • For example, we draw a red regular octagon.
  • The procedure is as follows:
t=0:2*pi/8:2*pi; %Take regular octagonal coordinate points
t=[t,t(1)]; %The beginning and end of the data vector coincide to make the graph closed
x=sin(t);
y=cos(t);
fill(x,y,'r'); %x, y are circular coordinates, the data interval is small enough to draw a circle
axis equal;
axis([-1.5,1.5,-1.5,1.5]);
  • The program running result is shown in the figure below.

3. Scatter graphics

  • Scatter graphics are often used in experiments to compare the differences between experimental results and theoretical values, and to study the law of errors according to the characteristic curve of experimental errors, and to obtain the relationship between the subject of research in the experiment and the related things of the subject. Relationship.
  • MATLAB provides the functions scatter, stairs and stem for drawing scatter graphics, which are used to draw scatter diagrams, ladder diagrams and stem diagrams respectively . The calling format of the three functions is as follows:
 scatter(x,y[,'filled'][,color])
    stairs(x,y,option)
    stem(x,y,options)
  • The usage of the 3 functions is similar to the plot function. In general, x and y are equal-sized vectors used to locate data points. The filled option of the scatter function means to fill the plot points, when omitted, the data points are hollow.
  • For example, we plot the curves as scatter plots, step charts and bar charts

    the y

    =

    2

    e

    ?

    0.5

    x

    y=2e^{-0.5x}

    y=2e?0.5x.

  • The procedure is as follows:
x=0:0.35:7;
y=2*exp(-0.5*x);
subplot(1,3,1);
scatter(x,y,'green');
title('scatter(x,y,''g'')');
axis([0,7,0,2]);
subplot(1,3,2);
stairs(x,y,'b');
title('stairs(x,y,''b'')');
axis([0,7,0,2]);
subplot(1,3,3);
stem(x,y,'k');
title('stem(x,y,''k'')');
axis([0,7,0,2]);
  • The program running result is shown in the figure below.

4. Vector graphics

  • A vector is specified by two arguments representing the x and y components of the vector, or a complex number whose real and imaginary parts represent the x and y components of the vector, respectively. Vector graphs include compass graphs, feather graphs, and arrow graphs, and MATLAB provides functions for drawing these graphs.
  • (1) Compass chart. A compass chart represents a vector originating at the origin of the coordinates, while also showing circular dividers in the coordinate system. The function to draw this kind of graphics is compass, and its calling format is as follows.
  • compas(x,y): x, y are vectors of n elements, the function displays n arrows, the starting point of the arrow is the origin, and the position of the arrow is (x(i)), y( i)).
  • compass(z): z is a complex vector of n elements, the function displays n arrows, the starting point of the arrow is the origin, and the position of the arrow is (real(z), imag(z)). real(z) and imag(z) represent the real and imaginary parts of the complex vector, respectively.
  • (2) Feather diagram. A feather plot is a graphic that displays vectors equidistantly on the abscissa, which looks like a bird’s feather. The function to draw the feather map is feather, and its calling format is as follows.
  • feather(x,y): Draw the vector determined by x and y.
  • feather(z): Draw the vector determined by z, which is equivalent to feather(real(z),imag(z)).
  • (3) Arrow diagram. The graphs drawn by the above two functions can also be called arrow graphs, but the arrow graphs here can better represent vectors. The direction of the arrow represents the direction of the vector, and the length of the arrow represents the size of the vector. The function to draw this kind of graph is quiver, and its calling format is as follows:
 quiver([x,y,]u,v)
  • Among them, (x, y) is the starting point of the vector, and (u, v) is the vector to be drawn. x, y, u, and v must be matrices of the same shape. If x, y are omitted, thousands of points are uniformly taken as the starting point on the plane.
  • For example, we plot sinusoids as compass, feather, and arrow diagrams.
  • The procedure is as follows:
x=-pi:pi/8:pi;
y=sin(x);
subplot(2,2,1);
compass(x,y);
title('compass map');
subplot(2,2,2);
feather(x,y);
title('feather map');
subplot(2,1,2);
quiver(x,y);
title('arrow map');
  • The program running result is shown in the figure below.