Grouped Bar plot- colors for groups

9 Ansichten (letzte 30 Tage)
Simon
Simon am 8 Jul. 2014
Beantwortet: Aurele Turnes am 4 Aug. 2014
Dear all,
I have following plot:
ba=([P1,P2); %P_n is n x 1
bar(ba,'grouped');
now I want to seperately color the first and the second bar of each group with different colors. And each group gets a different set of colors, so that I have to use 2*n colors in total, which I have defined via RGB values beforehand.
How do I do this? I tried to work with set() and FaceColor, but it turned everything the same.
Thanks!

Antworten (1)

Aurele Turnes
Aurele Turnes am 4 Aug. 2014
If you are using MATLAB R2014a or earlier versions, you can recolor individual bars by first getting a handles to the bar plot as follows:
bar_handle = bar(ba,'grouped');
Then, access the groups by accessing the children of the handle above:
bar_group = get(bar_handle,'children');
Now, you can decide what colors you want each individual bar to have by giving them a color index and setting the 'FaceVertexCData' property for that group. For instance, if there are n bars in the first group, and you want the first bar to be a different color, you can do:
fvcd = ones(n,1);
fvcd(1) = 3;
set(bar_group{1},'FaceVertexCData',fvcd)
This is not supported in the MATLAB R2014b_Prerelease however.

Community Treasure Hunt

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

Start Hunting!

Translated by