0

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);
Roxana
  • 71
  • 4
  • Please do not repost questions. Edit your previous one, or this one, to add all the required information, but do not repeat them. I have now closed your older question. – Ander Biguri Oct 02 '19 at 14:16
  • In addition to that, please take your time to read what a [mcve] is. In this case, your example is not an [mcve], because its not reproducible (we do not have `X` nor `rgb`, we do not know how it looks) nor a good example for the same reasons. – Ander Biguri Oct 02 '19 at 14:17

1 Answers1

2

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

Here is a short example using bar(Y).
Resulting image

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

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.
Resulting image

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.

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
Michael
  • 535
  • 2
  • 6
  • Thank you Michael, it was very helpful. I really appreciate it. But the problem is when I test it with just one set of data it doesn't work properly. Can you please have a look at below code: Y = [.1 .25 .3 .7 .4 .5 6.3 .10 1.2 3.5 1.5]; h = bar(Y, 'hist'); set(h, 'CDataMapping', 'scaled'); h.CData = Y; colorbar(Y) I want to have indexed color for this set. Let's say 'jet'. – Roxana Oct 04 '19 at 01:17
  • It looks like your data Y is a row vector. I think the CData property need to be a column vector for your case. So when you try to assign CData, use h.CData = Y'. Let me know how it goes. – Michael Oct 05 '19 at 02:53
  • Thanks a lot Michael, it works now but only on random data, when I use real obs. it gives me one color bar chart not indexed color. – Roxana Oct 05 '19 at 05:25
  • Let's say X = [0.03 0.01 0.03 (0.02) (0.00) 0.01 (0.03) (0.01) 0.02 (0.04) (0.01) 0.03 (0.02) 0.01 0.03 0.01 0.00 (0.02) 0.02 (0.00) 0.01 (0.02) (0.05) 0.00 (0.01) 0.02 0.01 0.00 ] ' h = bar(X); set(h, 'FaceColor', 'flat'); h.CData = X'; colorbar(X) – Roxana Oct 05 '19 at 05:49
  • I also want to change the color set to something called "jet" – Roxana Oct 05 '19 at 05:50
  • I fixed thanks, I use the below codes: set(groot,'DefaultFigureColormap',jet); h = bar(Y,.99); set(h, 'FaceColor', 'flat'); h.CData = Y'; caxis([min(#) max(#)]); colorbar(N); – Roxana Oct 05 '19 at 13:08
  • In your example with X = [0.03 ...], the only missing bit is caxis. Alternatively, use set(gca, 'CLim', [#, #]). If you think the response answers your question, please select it as the accepted answer :) – Michael Oct 07 '19 at 05:39