ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Matlab 柱形图画法

2021-10-13 12:59:02  阅读:205  来源: 互联网

标签:subplot ch bar 画法 fvd fvcd 柱形图 set Matlab


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<br data-filtered="filtered">%% 直方图图的绘制

%直方图有两种图型:垂直直方图和水平直方图。而每种图型又有两种表现模式:累计式:分组式。

figure;

z=[3,5,2,4,1;3,4,5,2,1;5,4,3,2,5]; % 各因素的相对贡献份额

colormap(cool);% 控制图的用色

subplot(2,3,1);

bar(z);%二维分组式直方图,默认的为'group'

title('2D default');

subplot(2,3,2);

bar3(z);%三维的分组式直方图

title('3D default');

subplot(2,3,3);

barh(z,1);%分组式水平直方图,宽度设置为1

title('vert width=1');

subplot(2,3,4);

bar(z,'stack');%累计式直方图,例如:1,1+2,1+2+3构成了第一个bar

title('stack')

subplot(2,3,5);

bar3h(z,0.5,'stacked');%三维累计式水平直方图

title('vert width=1 stack');

subplot(2,3,6);

bar3(z,0.8,'grouped');%对相关数据的颜色进行分组,默认的位'group'

title('width=0.8 grouped');

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

%% =========柱状图的进阶==========

figure;

y=[300 311;390 425; 312 321; 250 185; 550 535; 420 432; 410 520;];

subplot(1,3,1);

b=bar(y);

grid on;

set(gca,'XTickLabel',{'0','1','2','3','4','5','6'})

legend('算法1','算法2');

xlabel('x axis');

ylabel('y axis');

%使仅有的一组柱状图呈现不同颜色,默认的位相同颜色

data = [1.0, 1.0, 0.565, 0.508, 0.481, 0.745];

subplot(1,3,2);

b = bar(data);

ch = get(b,'children');

set(ch,'FaceVertexCData',[4;2;3;1;5;6]);%使用Indexed形式指定每组bar的颜色

set(gca,'XTickLabel',{'C0','C1','C2','C3','C4','C5'})

axis([0 7 0.0 1.0]);

ylabel('micro F-measure');

%使每个bar颜色不同,默认的是每个元素在不同组的颜色相同

data = [3, 7, 5, 2;4, 3, 2, 9;6, 6, 1, 4];

subplot(1,3,3);

b = bar(data);

ch = get(b,'children');

set(ch{1},'FaceVertexCData',[1;2;3]);%设置第一个元素在不同组的颜色

set(ch{2},'FaceVertexCData',[1;2;3]);%设置第二个元素在不同组的颜色

set(ch{3},'FaceVertexCData',[1;2;3]);

set(ch{4},'FaceVertexCData',[1;2;3]);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

%% 彩色柱状图

%用到的数据

n = 8;

Z = rand(n,1);

figure;

%默认图片

subplot(1,3,1);

bar(Z);

%简单的作图

% 这个图根据数据列中值的大小着色。每列中的值越大,颜色越突出

subplot(1,3,2);

h=bar(Z);

colormap(summer(n));

ch = get(h,'Children');

fvd = get(ch,'Faces');%针对矩阵时,只能用fvd=get(ch{col},'Faces'),下同

fvcd = get(ch,'FaceVertexCData');

[~, izs] = sortrows(Z,1);

for i = 1:n

    row = izs(i);

    fvcd(fvd(row,:)) = i;

end

set(ch,'FaceVertexCData',fvcd)

%图片会以渐变的方式着色,效果非常不错

subplot(1,3,3);

h=bar(Z);

ch = get(h,'Children');

fvd = get(ch,'Faces');

fvcd = get(ch,'FaceVertexCData');

[zs, izs] = sortrows(Z,1);

k = 128; % 准备生成128 *3 行的colormap

colormap(summer(k)); % 这样会产生一个128 * 3的矩阵,分别代表[R G B]的值

% 检视数据

whos ch fvd fvcd zs izs

%   Name       Size            Bytes  Class     Attributes

%

%   ch         1x1                 8  double

%   fvcd      66x1               528  double

%   fvd       13x4               416  double

%   izs       13x1               104  double

%   zs        13x1               104  double

%

shading interp % Needed to graduate colors

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;

end

set(ch,'FaceVertexCData', fvcd); % Apply the vertex coloring

set(ch,'EdgeColor','k');

标签:subplot,ch,bar,画法,fvd,fvcd,柱形图,set,Matlab
来源: https://blog.csdn.net/qq_59747472/article/details/120741500

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有