MATLAB visualization (6) special two-dimensional images, how to draw bar charts, area charts, pie charts, histograms with matlab

1. Draw a bar graph

         The bar chart uses the functions bar and barh to draw vertical and horizontal two-dimensional bar charts respectively. Here is an example:

Y=round(rand(5,3)*10);   %随机产生一个5行3列的矩阵
subplot(2,2,1)   %设置子图,设定绘图区域,在左上角绘图
bar(Y,'group')   %设置纵向条形图,类型为grouped
legend('1','2','3')   %加上一个图例
title 'Group'  %为图像命名为Group
subplot(2,2,2)   %设置子图,设定绘图区域,在右上角绘图
bar(Y,'stacked','yellow')
title 'Stacked'
subplot(2,2,3)  %设置子图,设定绘图区域,在左下角绘图
barh(Y,'stack','red')
title 'Stack'
subplot(2,2,4)  %设置子图,设定绘图区域,在右下角绘图
bar(Y,7.5)  %设置每个条形宽度
title 'Width=7.5' 

     The grouped and stacked here are both used to indicate the style of the bar, the group is arranged side by side, and the stack is superimposed on a bar, just like stacking arhats. The color is the same as the color of the plot drawing, see the previous matlab visualization blog for details.

operation result:

 2. Area map area()

      By default, the function area centers the rows of the matrix, draws the curve and fills it.

For example: plot Y as an area graph, and each column of data is accumulated on the basis of the previous column.

%2 区域图area()

Y = [1,5,3;
     3,2,7;
     1,5,3;
     2,6,1];  %设置Y数据集
area(Y) %绘制向量Y或者矩阵Y各列的和 也可以area(X,Y)以X为横坐标 Y纵坐标
grid on  %加网格线
colormap summer   %加颜色
set(gca,'Layer','top') 
legend('因素1','因素2','因素3')
title 'ss'

  The bottom line is the data of the first column, the middle one is the accumulation of the data of the second and the first, and the third is the same.

3. Pie chart

% 3 饼形图 pie()
x=[1 3 0.5 2.5 2 ];
explode=[0 1 0 0 0]; %突出显示第二个元素,使其分离开 需要与数据维数相同
pie(x,explode)
colormap jet

 

4. Histogram

% 4 直方图  hist()
x = -6:0.1:6;
y = randn(10000,1);
%  hist(y) 对于y的直方图
hist(y,x)
h = findobj(gca,'Type','patch');
set(h,'Facecolor','r','EdgeColor','w')','w') %设置边界和颜色

 When n=hist(Y): Draw a histogram of Y

hist(Y,X): x is a vector, and the elements of x are the center to create a grid

 

Guess you like

Origin blog.csdn.net/m0_73982095/article/details/130752847