Changing color of individual bars in a grouped bar chart

11 ビュー (過去 30 日間)
Deepak Prakash K
Deepak Prakash K 2019 年 2 月 26 日
コメント済み: Fargol Rezayaraghi 2022 年 8 月 13 日
c = [25;55];
names = {'GR = 5.57' ; 'GR = 6.17'};
y = [604 603 ; 903 903 ; 1083 1083]';
h = bar(c,y,'FaceColor','flat');
set(gca, 'XTickLabel', names , 'TickLabelInterpreter', 'latex', 'FontSize' , 13); % , 'TickLabelInterpreter', 'latex'
ylabel({'\% contribution'},'Interpreter','latex', 'FontSize' , 14) % ,'Interpreter','latex'
xlim = get(gca,'xlim');
hold on
plot(xlim,[925 925],'LineStyle','--','Color','r','LineWidth',1.5)
In the code given above, I want to color those bars whose value is greater than 925 as red and those with less than 925 as green. I have refered to a lot of Matlab discussions regarding coloring of bar graph but nothing seems to work.
Is there a method also to have two x axis labels? Where I label each bar in a group and I label the group as well?
Thanks in advance.

採用された回答

Deepak Prakash K
Deepak Prakash K 2019 年 2 月 26 日
The question has been resolved. The following code helps to change color of each bar in the grouped bar plot.
c = [25;55];
names = {'GR = 5.57' ; 'GR = 6.17'};
y = [604 603 ; 903 903 ; 1083 1083]';
h = bar(c,y,'FaceColor','flat');
h(1).CData(1,:) = [0 1 0]; % group 1 1st bar
h(1).CData(2,:) = [0 1 0]; % group 1 2nd bar
h(2).CData(1,:) = [0 1 0]; % group 2 1st bar
h(2).CData(2,:) = [0 1 0]; % group 2 2nd bar
h(3).CData(1,:) = [1 0 0]; % group 3 1st bar
h(3).CData(2,:) = [1 0 0]; % group 3 2nd bar
  1 件のコメント
Fargol Rezayaraghi
Fargol Rezayaraghi 2022 年 8 月 13 日
Great! Thank you so much! I've been spending more than 12 hours to figure that out.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2019 年 2 月 26 日
YOu can use (bar,....','stacked')..
You need to play with inputs to get your value.
c = [25;55];
names = {'GR = 5.57' ; 'GR = 6.17'};
y = [604 603 ; 903 903 ; 1083 1083]';
h = bar(c,y,'stacked','FaceColor','flat');
set(gca, 'XTickLabel', names , 'TickLabelInterpreter', 'latex', 'FontSize' , 13); % , 'TickLabelInterpreter', 'latex'
ylabel({'\% contribution'},'Interpreter','latex', 'FontSize' , 14) % ,'Interpreter','latex'
xlim = get(gca,'xlim');
hold on
plot(xlim,[925 925],'LineStyle','--','Color','r','LineWidth',1.5)
  3 件のコメント
KSSV
KSSV 2019 年 2 月 26 日
i got your question....you have to change your input accordingly...
Deepak Prakash K
Deepak Prakash K 2019 年 2 月 26 日
Can you please elaborate on your comment? What do you mean change the input accordingly? What input are you refering to?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by