天天看点

matlab bar3 颜色,matlab中怎么控制柱状图标注的颜色?

满意答案

matlab bar3 颜色,matlab中怎么控制柱状图标注的颜色?

lastdargo

推荐于 2016.01.18

matlab bar3 颜色,matlab中怎么控制柱状图标注的颜色?

采纳率:45%    等级:10

已帮助:818人

一、利用bar3画一个矩阵的柱状图时,如何改变显示的颜色,让它根据数据大小的不同显示不同的颜色,比如数据越大颜色越深,而不是bar3内置的颜色那样沿x轴或y轴渐变。可以试着用以下的方法实现:

M=rand(30,20);

figure

subplot(1,2,1)

h=bar3(M)

for n=1:numel(h)

cdata=get(h(n),'zdata');

set(h(n),'cdata',cdata,'facecolor','interp')

end

subplot(1,2,2)

h=bar3(M)

for n=1:numel(h)

cdata=get(h(n),'zdata');

cdata=repmat(max(cdata,[],2),1,4);

set(h(n),'cdata',cdata,'facecolor','flat')

end

二、用以下的代码实现

% 彩色柱状图

%% 用到的数据

n = 13;

Z = rand(n,1);

%% 默认图片

bar(Z);

%% 简单的作图

% 这个图根据数据列中值的大小着色。每列中的

% 值越大,颜色越突出

figure

h=bar(Z);

colormap(summer(n));

ch = get(h,'Children');

fvd = get(ch,'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)

%% 更加漂亮的图片

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

figure

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')

00分享举报