How to select different colors and legend for the bar plot??

80 ビュー (過去 30 日間)
BN
BN 2020 年 7 月 27 日
コメント済み: BN 2020 年 7 月 29 日
Hello everyone, I plot this bar plot in order to show the difference between each model (3 models) in condition A and condition B. x = rand(3,2);
x = rand(3,2);
h = bar (x);
I want to set, for example, red for the first model in condition A and blue for condition B (In this model).
As well as two different colors like green and yellow for the second model in each condition. Like this plot below:
And also, set the corresponding legend for each color. So I want to have 6 items in legend (different colors).
Any suggestion is really helpful
Thank you.

採用された回答

Turlough Hughes
Turlough Hughes 2020 年 7 月 27 日
編集済み: Turlough Hughes 2020 年 7 月 27 日
For the first part of your question, you can do so as follows:
x = rand(3,2);
h = bar (x);
h(1).FaceColor = 'red'
h(2).FaceColor = 'blue'
For the second part, patterned face colours aren't provided for with the bar() function. When you plot a bar chart such as:
h = bar(rand(3,6))
you can continue to modify the colors of any model using the same procedure as above. Additionally the legend will update for you accordingly:
legend('model A','model B','model C','model D','model E','model F')
  3 件のコメント
Turlough Hughes
Turlough Hughes 2020 年 7 月 27 日
編集済み: Turlough Hughes 2020 年 7 月 27 日
You could do the following:
data = rand(1,6);
groupSize = 2;
figure(), hold on
x = 0;
for c = 1:numel(data)
x = x + 1;
b(c) = bar(x,data(c));
if mod(c,groupSize)==0 %after each group skip a position on the x axis
x = x + 1;
end
end
spacing = groupSize+1;
ax = gca;
ax.XTick = spacing/2:spacing:v-spacing/2;
ax.XTickLabel = {'model 1','model 2','model 3'};
Turlough Hughes
Turlough Hughes 2020 年 7 月 27 日
You can modify the individual bars then as follows:
b(3).FaceColor = 'g';
b(4).FaceColor = 'm';
b(5).FaceColor = 'k';
b(6).FaceColor = 'y';

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

その他の回答 (1 件)

Sugar Daddy
Sugar Daddy 2020 年 7 月 27 日
編集済み: Sugar Daddy 2020 年 7 月 27 日
2nd Part of Question (Pattern Face)
This code is computation intesive and is just for display purpose. (considering that max value is less than 1)
figure,
x = rand(3,2);
c = [1 2 3];
h = bar (c,x,'BarWidth',1);
h(2).FaceColor = 'none';
h(2).LineWidth = 2;
%
jj = 1;
hold on
%
a =gca; clr_array = a.ColorOrder;
for mm = 1:3
for i = x(mm,2):-.01:0
if(jj == 1)
jj = 2;
bar(mm,[0;i],'FaceColor','w','EdgeColor','none','BarWidth',1)
else
bar(mm,[0;i],'FaceColor',clr_array(mm,:),'EdgeColor','none','BarWidth',1)
jj = 1;
end
end
bar(mm,[x(mm,1);0],'FaceColor',clr_array(mm,:),'LineWidth',2,'BarWidth',1)
bar(mm,[0;x(mm,2)],'FaceColor','none','LineWidth',2,'BarWidth',1)
end
Regards,
Sugar Daddy

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by