尝试在MATLAB中将颜色图应用于条形图 [英] Trying to apply a color map to a bar graph in MATLAB
问题描述
我有一组试图将其绘制为直方图的数据.另外,我想根据x轴位置为各个条着色. CData ,这里描述的似乎可以满足我的要求,但我无法正常工作.
I have a collection of data that I am trying to graph as a histogram. Additionally, I would like to color the individual bars as a function of the x axis location. CData, described here seems to do what I want but I can't get it to work.
这是我的代码:
h = bar(new_edge,N,'hist','FaceColor','flat');
hold on
for n = 1:length(N)
if (x - x/1.09) - (x-1) > 0
probability(n) = 1 - ((x-x/1.09) - (x-1))/((x - 1/1.09)+(x/0.91 - x));
else
probability(n) = 1;
end
color_num = 30;
cm = jet(color_num);
min = 0.5450;
max = 1;
color_map_index = floor(1 + (probability(n) - min)/(max-min)*(color_num-1));
rbg = cm(color_map_index,:);
h.CData(n,:) = rbg;
end
类似于MATLAB示例,我首先创建我的条形图.接下来,我要遍历并根据计算为每个条指定颜色.为此,我创建了一个带有#个垃圾箱和一个最小/最大值的颜色图,获取了颜色索引,然后最终获取了rbg值.尝试应用颜色时出现以下错误:
Similar to the MATLAB example, I first create my bar graph. Next, I want to loop through and prescribe the color for each bar based on a calculation. I do this by creating a colormap with # of bins and a min/max, getting a color index, then finally retrieving the rbg value. I get the following error when I try to apply the color:
下标索引必须是实数正整数或逻辑.
h.CData(n,:) = rbg;
如果我深入研究h对象,MATLAB会告诉我CData的大小为(4x65).这里发生了什么? new_edge
和N
都是1x65向量.
If I dive into the h object, MATLAB tells me that CData has a size of (4x65). What's going on here? Both new_edge
and N
are 1x65 vectors.
推荐答案
这可能与您的Matlab版本有关. 当我在2017b上使用bar测试CData时,此方法有效:
This could be a problem with your Matlab version. When I test CData with bar on 2017b, this works:
openExample('graphics/ControlIndividualBarColorsExample')
当我在2017a上尝试时,它没有运行. 这个例子有用吗?
When I try it on 2017a, it doesn't run. Does the example work?
这篇关于尝试在MATLAB中将颜色图应用于条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!