필터 지우기
필터 지우기

Grouped Bar plot- colors for groups

조회 수: 5 (최근 30일)
Simon
Simon 2014년 7월 8일
답변: Aurele Turnes 2014년 8월 4일
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!

답변 (1개)

Aurele Turnes
Aurele Turnes 2014년 8월 4일
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.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by