Matlab 条形:使用颜色图(喷射)将颜色设置为条形高度的函数 [英] Matlab bar : set colors with colormap(jet) as a function of bar heights
问题描述
我有 4 组不同的多条.我想,对于每个组,用 colormap(jet)
绘制每个条形的颜色,该颜色是其高度值(即相关条形的值)的函数.
目前,我已经完成了:
h=bar(xpoints(1:4),bpcombined(1:4,:),'grouped','BarWidth',0.5);颜色条;颜色图(喷射);
我得到下图:
如您所见,对于每个条形,其颜色不是高度的函数:它只是一系列不同颜色(喷气颜色图),4 组相同.
我怎样才能获得这种依赖性,我的意思是在 colormap(jet) 之后最高的条是红色的,最低的蓝色是?
更新 1 : 我尝试了 @masei
建议的解决方案,但在 Matlab 2016b 上出现以下错误:
类matlab.graphics.chart.primitive.Bar"没有合适的方法、属性或字段CData".test_plot_bar_color 中的错误(第 17 行)b.CData(k,:) = y_color(k,:);
我不知道如何规避这个错误.
@masei
告诉我使用‘FaceColor
’属性,但我不知道如何将它放在这个脚本上.
欢迎任何帮助
更新 2:
我尝试应用@marsei
的建议,即多次绘制条形组的部分(其他组设置为Nan),如下所示:
1 清除;2% 数据3 y = rand(10,4);4 % 2D nan 数组5 y_nan = nan(大小(y))默认 6 % 一维数组7 y_1d(1:numel(y)) = nan(numel(y),1);89%情节10 数字('颜色', 'w');11 title('带有高度依赖颜色的条形');12 k = 1:size(y,2)13 % 设置默认一维数组14 y_temp = y_1d;15 % 放入第 k 部分数据16 y_temp((k-1)*size(y_nan,1)+1:k*size(y_nan,1)) = y(1:size(y_nan,1),k);17 % 将 1D 重塑为 2D 以用于条形图18 y_final = reshape(y_temp,size(y,2),size(y,1));19 % 绘图栏20 bar_h = bar(y_final,'BarWidth',0.5);21 y_color = vals2colormap(y_final(k,:),'jet');22 %b.FaceColor = y_color(:,:);23 %y_color24 bar_child=get(bar_h,'儿童');25 bar_child.FaceColor = y_color(:,:);26%colormap(y_color);27%set(bar_child,'CData',y_final);28 坚持;29 结束30
但我收到以下错误:
对非结构数组对象的字段分配.test_plot_bar_color 中的错误(第 25 行)bar_child.FaceColor = y_color(:,:);
有人能看出我的做法有什么问题吗?
问候
EDIT - 使用排序数据的示例 (y = sort(rand(30,10),2)
和 分离间隙 = 5
)
您可以使用名为 CData
的常规条形属性来为每个条形分配一种颜色.在 vals2colormap
的帮助下计算颜色(
由
制作% 数据y = rand(10,4);% 添加 NaN(用于分离)并展平分离间隙 = 1;% 2 列 NaNy_nan = [y nan(size(y,1),separation_gap)]';y_flat = [nan(separation_gap,1);y_nan(:)];% 将值转换为 colomapy_color = vals2colormap(y_flat, 'jet');%plot 和 cange 颜色图('颜色','w');b = bar(y_flat);%,'分组'b.FaceColor = '平面';对于 k = 1:size(y_flat,1)b.CData(k,:) = y_color(k,:);结尾title('带有高度依赖颜色的条形');
I have 4 different groups of multiple bars. I would like, for each group, to plot each bar with a color which is function of its height value (i.e the value of the concerned bar) with colormap(jet)
.
For the moment, I have done :
h=bar(xpoints(1:4),bpcombined(1:4,:),'grouped','BarWidth',0.5);
colorbar;
colormap(jet);
and I get the following figure :
As you can see, for each bar, its color is not a function of the height : it is just a sequence of different colors (of jet colormap), the same for the 4 groups.
How can I get this dependency, I mean the highest bars would be red and the lowest blue following colormap(jet) ?
UPDATE 1 : I tried the solution suggested by @masei
but I get the following error on Matlab 2016b :
No appropriate method, property, or field 'CData' for class 'matlab.graphics.chart.primitive.Bar'.
Error in test_plot_bar_color (line 17)
b.CData(k,:) = y_color(k,:);
I don't how to circumvent this error.
@masei
told me to use ‘FaceColor
’ property but I don't know how to put it on this script.
Any help is welcome
UPDATE 2 :
I tried to apply the suggestion of @marsei
, i.e plot multiple times the sections of group of bar (and the other groups are set to Nan), like this :
1 clear;
2 % Data
3 y = rand(10,4);
4 % 2D array of nan
5 y_nan = nan(size(y))
6 % 1D array by default
7 y_1d(1:numel(y)) = nan(numel(y),1);
8
9 %Plot
10 figure('Color', 'w');
11 title('Bar with height-dependant color');
12 for k = 1:size(y,2)
13 % Set default 1D array
14 y_temp = y_1d;
15 % Put k-th part of data
16 y_temp((k-1)*size(y_nan,1)+1:k*size(y_nan,1)) = y(1:size(y_nan,1),k);
17 % Reshape 1D into 2D for bar
18 y_final = reshape(y_temp,size(y,2),size(y,1));
19 % Plot bar
20 bar_h = bar(y_final,'BarWidth',0.5);
21 y_color = vals2colormap(y_final(k,:),'jet');
22 %b.FaceColor = y_color(:,:);
23 %y_color
24 bar_child=get(bar_h,'Children');
25 bar_child.FaceColor = y_color(:,:);
26 %colormap(y_color);
27 %set(bar_child,'CData',y_final);
28 hold on;
29 end
30
But I get the following error :
Field assignment to a non-structure array object.
Error in test_plot_bar_color (line 25)
bar_child.FaceColor = y_color(:,:);
Anyone could see what's wrong in my way to do ?
Regards
EDIT - An example that uses sorted data (y = sort(rand(30,10),2)
and separation_gap = 5
)
You can use a regular bar property called CData
to assign each bar a color. The colour is computed with the help of vals2colormap
(web).
The initial data matrix has 10 rows, that gives 10 groups of bars in regular bar
plot. At this point, changing the CData
property for the first bar changes the color of all the first bar (for all groups). You can't dissociate the first bars.
The trick is then to flatten the matrix while inserting NaNs for group separation. Here, the initial data is 10x4
flatten in a 40 element vector, separated with a single Nan
. With this, you have a full control over each individual bar (bars are not grouped anymore).
The following graph
is produced by
% data
y = rand(10,4);
% add NaNs (for separation) and flatten
separation_gap = 1; % 2 columns of NaN
y_nan = [y nan(size(y,1), separation_gap)]';
y_flat = [nan(separation_gap,1); y_nan(:)];
%convert values to colomap
y_color = vals2colormap(y_flat, 'jet');
%plot and cange color
figure('Color', 'w');
b = bar(y_flat);%,'grouped'
b.FaceColor = 'flat';
for k = 1:size(y_flat,1)
b.CData(k,:) = y_color(k,:);
end
title('Bar with height-dependant color');
这篇关于Matlab 条形:使用颜色图(喷射)将颜色设置为条形高度的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!