Matlab高级绘图

来源:互联网 发布:手机短信软件 编辑:程序博客网 时间:2024/06/05 17:23

x
  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');
复制代码
  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');
复制代码

复制代码
  1. %% =========柱状图的进阶==========
  2. figure;
  3. y=[300 311;390 425; 312 321; 250 185; 550 535; 420 432; 410 520;];
  4. subplot(1,3,1);
  5. b=bar(y);
  6. grid on;
  7. set(gca,'XTickLabel',{'0','1','2','3','4','5','6'})
  8. legend('算法1','算法2');
  9. xlabel('x axis');
  10. ylabel('y axis');
  11. %使仅有的一组柱状图呈现不同颜色,默认的位相同颜色
  12. data = [1.0, 1.0, 0.565, 0.508, 0.481, 0.745];
  13. subplot(1,3,2);
  14. b = bar(data);
  15. ch = get(b,'children');
  16. set(ch,'FaceVertexCData',[4;2;3;1;5;6]);%使用Indexed形式指定每组bar的颜色
  17. set(gca,'XTickLabel',{'C0','C1','C2','C3','C4','C5'})
  18. axis([0 7 0.0 1.0]);
  19. ylabel('micro F-measure');
  20. %使每个bar颜色不同,默认的是每个元素在不同组的颜色相同
  21. data = [3, 7, 5, 2;4, 3, 2, 9;6, 6, 1, 4];
  22. subplot(1,3,3);
  23. b = bar(data);
  24. ch = get(b,'children');
  25. set(ch{1},'FaceVertexCData',[1;2;3]);%设置第一个元素在不同组的颜色
  26. set(ch{2},'FaceVertexCData',[1;2;3]);%设置第二个元素在不同组的颜色
  27. set(ch{3},'FaceVertexCData',[1;2;3]);
  28. set(ch{4},'FaceVertexCData',[1;2;3]);
复制代码
 

  1. %% 彩色柱状图
  2. %用到的数据
  3. n = 8;
  4. Z = rand(n,1);
  5. figure;
  6. %默认图片
  7. subplot(1,3,1);
  8. bar(Z);
  9. %简单的作图
  10. % 这个图根据数据列中值的大小着色。每列中的值越大,颜色越突出
  11. subplot(1,3,2);
  12. h=bar(Z);
  13. colormap(summer(n));
  14. ch = get(h,'Children');
  15. fvd = get(ch,'Faces');%针对矩阵时,只能用fvd=get(ch{col},'Faces'),下同
  16. fvcd = get(ch,'FaceVertexCData');
  17. [~, izs] = sortrows(Z,1);
  18. for i = 1:n row = izs(i); fvcd(fvd(row,:)) = i;
  19. end
  20. set(ch,'FaceVertexCData',fvcd)
  21. %图片会以渐变的方式着色,效果非常不错
  22. subplot(1,3,3);
  23. h=bar(Z);
  24. ch = get(h,'Children');
  25. fvd = get(ch,'Faces');
  26. fvcd = get(ch,'FaceVertexCData');
  27. [zs, izs] = sortrows(Z,1);
  28. k = 128; % 准备生成128 *3 行的colormap
  29. colormap(summer(k)); % 这样会产生一个128 * 3的矩阵,分别代表[R G B]的值
  30. % 检视数据
  31. whos ch fvd fvcd zs izs
  32. % Name Size Bytes Class Attributes
  33. %
  34. % ch 1x1 8 double
  35. % fvcd 66x1 528 double
  36. % fvd 13x4 416 double
  37. % izs 13x1 104 double
  38. % zs 13x1 104 double
  39. %
  40. shading interp % Needed to graduate colors
  41. for i = 1:n color = floor(k*i/n); % 这里用取整函数获得color在colormap中行 row = izs(i); % Look up actual row # in data fvcd(fvd(row,1)) = 1; % Color base vertices 1st index fvcd(fvd(row,4)) = 1; fvcd(fvd(row,2)) = color; % Assign top vertices color fvcd(fvd(row,3)) = color;
  42. end
  43. set(ch,'FaceVertexCData', fvcd); % Apply the vertex coloring
  44. set(ch,'EdgeColor','k');
复制代码
  1. %% 绘制统计直方图
  2. %hist(y):如果y是向量,则把其中元素放入10个条目中,且返回每条中的元素的个数;如果y为矩阵,则分别对每列进行处理,显示多组条形。
  3. %[n,xout]=hist(y,x):非递减向量x的指定bin的中心。向量xout包含频率计数与条目的位置。
  4. x=-10:.1:10;
  5. y1=randn(2008,1);
  6. y2=randn(2008,3);
  7. figure;
  8. colormap(winter);
  9. subplot(2,2,1);
  10. hist(y1);%把其中元素放入10个条目中
  11. title('y1为向量,default,n=10');
  12. subplot(2,2,2);
  13. hist(y2);%分别对每列进行处理,显示多组条形
  14. title('y2为矩阵');
  15. subplot(2,2,3);
  16. hist(y1,x);%用户也可以使用[n,xout]=hist(y1,x);bar(xout,n)绘制条形直方图
  17. title('向量x指定条目');
  18. subplot(2,2,4);
  19. hist(y2,1000);%第二个参数为标量时指定bin的数目
  20. title('nbins=1000');
复制代码
 

  1. %% ========均值方差直方图========
  2. a=[8 9 10 7 8 9];%mean
  3. b=[1 1 1 1 1 1];%std
  4. figure();
  5. h=bar(a);
  6. ch=get(h,'children');
  7. set(ch,'FaceVertexCData',[4;2;3;1;5;6]);%使用Indexed形式指定每组bar的颜色
  8. hold on;
  9. errorbar(a,b,'k','LineStyle','none');
复制代码
 

  1. %% =======散点图scatter , scatter3 , plotmatrix======
  2. %scatter3(X,Y,Z,S,C):在由向量X、Y和Z指定的位置显示大小和颜色分别由S和C决定的离散点
  3. figure;
  4. [x,y,z] = sphere(16);
  5. X = [x(:)*.5 x(:)*.75 x(:)];
  6. Y = [y(:)*.5 y(:)*.75 y(:)];
  7. Z = [z(:)*.5 z(:)*.75 z(:)];
  8. S = repmat([10 2 5]*10,numel(x),1);
  9. C = repmat([1 2 3],numel(x),1);
  10. subplot(1,2,1);
  11. scatter(X(:),Y(:),S(:),C(:));
  12. title('scatter');
  13. subplot(1,2,2);
  14. scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled'), view(-60,60);
  15. title('scatter3');
  16. %plotmatrix(X,Y)绘出X(p*M)与Y(p*N)的列组成的散度图(N,M)
  17. figure;
  18. X=randn(100,2);Y=randn(100,2);
  19. subplot(1,3,1),plotmatrix(X);%等价于plotmatrix(X,X),除了对角上的图为X每一列的直方图hist(X(:,col))
  20. title('plotmatrix(X)');
  21. subplot(1,3,2),plotmatrix(X,X);
  22. title('plotmatrix(X,X)');
  23. subplot(1,3,3),plotmatrix(X,Y);
  24. title('plotmatrix(X,Y)');
复制代码
 

    

  1. %% =========绘制区域图===========
  2. %区域图特点是:在图上绘制多条曲线时,每条曲线(除第一条外)都是把“前”条曲线作基线,再取值绘制而成。因此,该指令所画的图形,能醒目地反映各因素对最终结果的贡献份额。
  3. figure;
  4. x=1:2:9;% 注意:自变量要单调变化
  5. y=magic(5);% 各因素的相对贡献份额,每一列相当于一个因素
  6. colormap(spring);% 控制图的用色
  7. area(x,y,4);%area(y)则以列下标作为自变量,第三个参数为基准线(默认为0)
  8. set(gca,'layer','top');%图层设置为top层,显示网格
  9. title('basevalue=4');
  10. legend(' 因素 A',' 因素 B',' 因素 C','因素D','因素E');
  11. grid on;
复制代码
 

  1. %% =========绘制饼状图=========
  2. %饼图指令pie和pie3用来表示各元素占总和的百分数。该指令第二个参数为与第一参数等长的 0-1
  3. %向量,1使对应扇块突出。第三个参数指定个扇区的label
  4. figure;
  5. colormap(summer);% 控制图的用色
  6. x=[16 17 21 25 21];
  7. subplot(1,2,1);
  8. pie(x,[0 0 0 0 1],{'0-10岁儿童','10-20岁儿童','20-35岁青年','35-55岁中年','55岁以上老年'});
  9. subplot(1,2,2);
  10. pie3(x,[0 0 0 0 1],{'0-10岁儿童','10-20岁儿童','20-35岁青年','35-55岁中年','55岁以上老年'});
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');0
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');1
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');2
复制代码
 

    

    

    

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');3
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');4
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');5
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');6
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');7
复制代码
 

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');8
复制代码
 

    

    

  1. %% 直方图图的绘制
  2. %直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。
  3. figure;
  4. z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额
  5. colormap(cool);% 控制图的用色
  6. subplot(2,3,1);
  7. bar(z);%二维分组式直方图,默认的为'group'
  8. title('2D default');
  9. subplot(2,3,2);
  10. bar3(z);%三维的分组式直方图
  11. title('3D default');
  12. subplot(2,3,3);
  13. barh(z,1);%分组式水平直方图,宽度设置为1
  14. title('vert width=1');
  15. subplot(2,3,4);
  16. bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar
  17. title('stack')
  18. subplot(2,3,5);
  19. bar3h(z,0.5,'stacked');%三维累计式水平直方图
  20. title('vert width=1 stack');
  21. subplot(2,3,6);
  22. bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'
  23. title('width=0.8 grouped');9
复制代码
 

    
0 0
原创粉丝点击