MATLAB drawing (2)

1. Bar graph drawing

        Drawing bar graphs can be divided into two-dimensional and three-dimensional cases: bar (vertical bar graph), barh (horizontal bar graph), bar3 (three-dimensional vertical bar graph), bar3h (three-dimensional horizontal bar graph) , the format of the function call in these cases is the same:

bar(y): use y as the component to display the height respectively, and the abscissa is [1, length(y)]; if y is a matrix, the situation is similar;

bar(x,y): draws y on the specified x, where x is a strictly single-increasing vector;

bar(...,width): width is the spacing of the bars in the group, the default value is 0.8 (very small spacing), if you set width=1, the bars in the group will touch each other;

bar(...,'style'): style specifies the type of arrangement, there are "group" (default) and "stack". If Y is a matrix of m*n size, group means that each group has n bars and m groups, and stack means that there are m bars and each color is different, and the height of each bar is the sum of the row vectors.

x = [1:10];
y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];
figure(2);
bar(x,y), xlabel('Student'),ylabel('Score'),
title('First Sem:')

2. Pie chart drawing

        Pie charts are used to display the proportion of each element in a vector or matrix. Two-dimensional is pie, three-dimensional is pie3, and the calling format is similar.

pei(X): Draw a pie chart with the data in X. If sum(X)<1, the pie chart drawn is incomplete;

pei(X,explode): Offset each part of the sector by a certain position, where explode is a matrix with the same dimension as X. When all elements are 0, each part of the sector will form a circle.

X = [528 718 624 859];
explode = [0 1 0 1];
pie3(X,explode);
title('三维分离饼图');

3. Area chart drawing

        The drawing of the area chart can actually show the influence of different parts on the whole, and its command is area.

area(Y): Draw each column in the vector or matrix Y as a separate curve and stack it;

area(X,Y): Draw a graph of Y versus X, filling the area between 0 and Y;

area(...,basevalue): basevalue specifies the base value for filling the area, and the default is 0.

Y = [45 4 6; 24 6 9; 19 4 3; 9 2 4; 25 8 15; 7 14 9];
area(Y);
grid on;
set(gca,'layer','top');    %将坐标的图层上移到顶层
title('面积图');

4. Drawing of contour lines

        [C, h] = contour(x,y,g);            
returns the contour matrix C (containing the data defining the contours) and the Contour object h. Plot the contours of z with x,y.

        meshgrid function: Generate two arrays:
one is y column x row, each row is the same, each column is in order (-5, -4.9,..., 5)
one is x row y column, each column is Same, each row is (-3, -2.9,...,3)Τ in order        

[x,y] = meshgrid(-5:0.1:5,-3:0.1:3); 
g = x.^2 + y.^2;        %数组运算为要加  ‘.’
[C, h] = contour(x,y,g);            
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
% get(h,'LevelStep')*2,显示值为LevelStep的2倍的值,让画出来的等高线一条隔一条地显示

set and get are functions for setting and obtaining the attribute value of the handle object;
ShowText is on to display the value of the contour line, off is not to display the value of the contour line, and the default is off;
LevelStep is m to display the value in multiples of m Contours, obviously the larger m is, the sparser the drawn contours will be;
TextStep is n, which means the value of the contours whose value is a multiple of n will be displayed, and the values ​​of other contours will not be displayed;
 

5. Polar coordinate drawing

        The coordinates of the polar coordinate system are (ρ, θ)
polarplot draws points on polar coordinates;
polarscatter draws a scatter plot on polar coordinates;
polarhistogram draws a histogram on polar coordinates;
compass draws an image with arrows
on polar coordinates ; function plot on

figure(1);      
angle=0:0.1:2*pi;       
length1=2*angle;
polarplot(angle,length1,'--r'); 
figure(2);      
length2=sin(angle);
polarplot(angle,length2,':b');
%极坐标中不支持在绘图函数中设定自变量的范围,可调用函数rlim
figure(3);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
polarplot(angle,length2,'b');
rlim([-1 1]);
figure(4);
polarscatter(angle,length1,'filled');   %不添加 filled 命令则默认为空心圆点
figure(5);
polarhistogram(length2)     %直方图是对数据点进行统计,然后绘制频数
figure(6);
compass([1 0 0 -1],[0 1 -2 1]);     %两个向量,第一个是四个横坐标,第二个是对应的纵坐标
%例如第一个箭头在平面坐标上(0,0)指向(1,0),转换到极坐标就是θ=0,ρ=1
figure(7);
ezpolar(@(angle)2*angle)
figure(8);
ezpolar(@sin);

6. Drawing of 3D graphics

        Most of the function formats for drawing three-dimensional graphics are similar to two-dimensional, except that a third-dimensional information is added to the parameters.

surf : plot a surface mesh : plot a mesh
surfc : plot a surface with contours meshc : plot a mesh with contours meshz
                                                              : plot a mesh with curtains
fsurf : plot a function-based surface fmesh : draw function-based

        The usage format of the above functions is similar, just take the mesh function as an example:

mesh(X,Y,Z): draw a three-dimensional mesh, the color matches the height of the surface;

mesh(Z): The generated grid map satisfies X=1:n and Y=1:m, [n,m]=size(Z);

mesh(Z,c): Same as mesh(Z), and further specify the edge color by c.

x0=1:5;         
y0=6:11;            
[x,y]=meshgrid(x0,y0);   %把两个向量转变为两个矩阵
z=x.^3+y.^3;
figure(1);
c=randi(20,size(z));        %设定一个颜色变量,其大小要和函数 z 相同
surf(x,y,z,c,'linestyle','--');         %对线条颜色设定时要通过 属性对linestyle 进行设置
colorbar;   %插入颜色栏
figure(2);          
h=surf(x,y,z);      %获取图像的句柄
h.FaceAlpha=0.5;    %调整透明度 1-完全不透明  0-完全透明
figure(3);          
surfc(x,y,z);   %下方的曲线就是等高线
figure(4);          
mesh(x,y,z); 
figure(5);         
meshc(x,y,z); 
figure(6);          
meshz(x,y,z);   
figure(7);              
z=@(x,y)x.^3+y.^3;
h=fsurf(z);       %会把数据点取的很密
h.EdgeColor=[1 0 0];            
h.LineStyle=('--');
figure(8);              
z=@(x,y)x.^3+y.^3;
fmesh(z);

Guess you like

Origin blog.csdn.net/weixin_58351753/article/details/127165374