我想根据值/高度为条形图着色 [英] I want to color bar chart based on values/heights

查看:102
本文介绍了我想根据值/高度为条形图着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据其值使用具有颜色的条形图.我可以将条形图和热图分开,但不能分开.

I am trying to have bar chart that has color according to its value. I can get the bar chart and heatmap separate but not together.

以下是我尝试合并的命令:

Here are the commands I try to combine:

N = X(1:10,1);
h = bar(N, 'hist');
cData = get(h, 'CData');
h.CData(n,:) = rbg;
set(h, 'CData', cData);

推荐答案

我在R2018b版本中进行了测试.条形的颜色默认情况下由FaceColorEdgeColor属性确定,而不直接由CData属性确定. CData属性仅在FaceColor设置为'flat'时使用.

I tested in version R2018b. The colours of the bars are determined by the FaceColor and EdgeColor properties by default, not directly by the CData property. The CData property is only used when FaceColor is set to 'flat'.

这是使用bar(Y)的简短示例.

Here is a short example using bar(Y).

n_groups = 4;
n_data_per_group = 5;

Y = rand(n_groups, n_data_per_group);

h = bar(Y);
set(h, 'FaceColor', 'flat');
for i = 1:n_data_per_group
    h(i).CData = Y(:, i);
end

colorbar

或者,如另一线程中gnovice的解决方案所建议的那样,一个人可以使用bar(Y, 'hist')来制作Patch对象而不是Bar对象.下面给出了他的示例中的一些细微修改,以适应多组数据.

Alternatively, as suggested by gnovice's solution in the other thread, one can use bar(Y, 'hist') to make Patch objects instead of Bar objects. A slight modification in his example to accommodate for multiple groups of data is given below.

n_groups = 4;
n_data_per_group = 5;

Y = rand(n_groups, n_data_per_group);
h = bar(Y, 'hist');
set(h, 'CDataMapping', 'scaled');
for i = 1:n_data_per_group
    h(i).CData = Y(:, i);
end

colorbar

两种方法之间可观察到的主要区别是条之间的默认间距.

The main observable difference between the two methods is the default spacing between the bars.

这篇关于我想根据值/高度为条形图着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆