anyway to give separate color to 'stack' bar plot

Hi:
I read an post from link below:
which provide a way to add annotation to the individual of each data. I would like to know if there is anyway to control the color of each individual bar?
untitled.jpg
Thanks!
Yu

Answers (1)

darova
darova on 16 Oct 2019
Edited: darova on 16 Oct 2019
Do you have colors you want to paint bars into?
for i = 1:length(hBar)
h1 = get(hBar(i),'Children');
n = length( get(h1,'FaceVertexCdata') );
set(h1,'faceVertexCdata',rand(n,3))
end

5 Comments

Hi:
thanks for your reply. I get an empty output by using:
h1 = get(hBar(1),'Children')
but the output of hBar(i) is not:
>> hBar(i)
ans =
Bar with properties:
BarLayout: 'stacked'
BarWidth: 0.8000
FaceColor: [0 0.4470 0.7410]
EdgeColor: [0 0 0]
BaseValue: 0
XData: [1 2 3 4 5]
YData: [8 10 7 1 9]
Thanks!
Yu
I think a have older version of MATLAB
for i = 1:length(hBar)
n = length( get(hBar(i),'FaceVertexCdata') );
set(hBar(i),'faceVertexCdata',rand(n,3))
end
Yu Li
Yu Li on 16 Oct 2019
Edited: Yu Li on 16 Oct 2019
Hi:
I'm sorry it still not work, the available variable relate to 'face' is: 'facecolor' and 'facealpha'.
may I know your version? I tried both 2018a and 2019a, both of them not work.
Bests,
Yu
I have v2013. Which face will change if you try this
hBar(1).FaceColor = [1 0 0];
Hi:
yes, 'faceColor' works but it is only single color. I need to change all the colors.
Thanks!
Yu

Sign in to comment.

Asked:

on 16 Oct 2019

Commented:

on 16 Oct 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!