Fill boxplot with different colors using RGB-Codes

5 ビュー (過去 30 日間)
Laura Neher
Laura Neher 2020 年 7 月 26 日
編集済み: Laura Neher 2020 年 7 月 29 日
I am new to Matlab but managed to create grouped boxplots and want to color them. I tried it with the code above. It works with:
color = ['c','b','c','b','c','b'];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(length(h)-j+1),'FaceAlpha',.5);
end
But I want to select special colors with RGB-Codes [11 166 222] and [0 178 169]. It works when I select just one color:
RGB=[11 166 222];
RGB2=[0 178 169];
color =[RGB;RGB;RGB;RGB;RGB;RGB];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
end
But when I attach the second color it creates totally different colors with RGB-Codes (157, 147, 208) and (254, 250, 142):
RGB=[11 166 222];
RGB2=[0 178 169];
color =[RGB;RGB2;RGB;RGB2;RGB;RGB2];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
end
What did I do wrong?
Do you know how I can get the colors I want?
Thank you so much.
If it is necessary, this is the whole code:
load 'FBE_.mat'
data = FBE_F1_30;
month = repmat({'Keine Rückmeldung','Positive Rückmeldung','Reale Rückmeldung'},1,2);
simobs = [repmat({'FBE'},1,3),repmat({'F1'},1,3)];
positions = [1 1.25 1.75 2 2.5 2.75];
a=boxplot(data,{month,simobs},'positions', positions,'colors',repmat('k',1,12),'factorgap',[5 3],'labelverbosity','minor','Symbol','ko');
xticks([1.125 2.125 3.125])
set(gca,'XTickLabel',{'Keine Rückmeldung','Positive Rückmeldung','Reale Rückmeldung'},'FontName','Arial','FontSize',11,'FontWeight','bold');
%Dicke des Medians ändern
set(a(6),'LineWidth',2);
set(a(13),'LineWidth',2);
set(a(20),'LineWidth',2);
set(a(27),'LineWidth',2);
set(a(34),'LineWidth',2);
set(a(41),'LineWidth',2);
%Boxplots einfärben
RGB1=[11 166 222];
RGB2=[0 178 169];
color = [RGB;RGB2;RGB;RGB2;RGB;RGB2];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(length(h)-j+1),'FaceAlpha',.5);
end
%Beschriftungen
title('Vergleich der tatsächlichen Ergebnisse der FBEs und vermeintlich verstandenen Wörter','FontName','Arial','FontSize',12,'Position',[2.25, 107, 0])
ylabel('Verständlichkeit in %','FontName','Arial','FontSize',12,'FontWeight','bold','Position',[0.06, 50, 0])
c = get(gca, 'Children');
hleg1 = legend(c(1:2), 'FBE', 'Frage1' );
%Textfeld
text(6.75,102.5,'n=17','FontName','Arial','FontSize',11,'FontWeight','bold')
%Y-Hilfslinien einzeichnen
set(gca,'YGrid','on','Layer','top')
%Größe des Ausgabefensters festlegen, damit Abstände an x-Achse passen
f = figure(1);
set(f,'Units','normalized','Position',[0.2 0.2 0.45 0.55]);

採用された回答

Cris LaPierre
Cris LaPierre 2020 年 7 月 27 日
編集済み: Cris LaPierre 2020 年 7 月 27 日
It looks like you already found this post? Note that RGB values range between 0 and 1, so divide your color vector by 255.
color =[RGB;RGB2;RGB;RGB2;RGB;RGB2]./255;
It also looks like you are doing the same thing Rik pointed out in his answer in that post. You need to pull in all 3 columns of color:
color(j,:)
  2 件のコメント
Laura Neher
Laura Neher 2020 年 7 月 27 日
Yes I saw that post, but found the code for color on an other website before.
I also so Riks answer an that he recommended to use
color(n,:)
But to be honest: I do not know where I have to insert it an what to do exactly.
I tried
RGB=[11 166 222];
RGB2=[0 178 169];
color = [RGB;RGB2;RGB;RGB2;RGB;RGB2]./255;
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),color(j,:),'FaceAlpha',.5);
end
but then I just get the first color for every boxplot.
Laura Neher
Laura Neher 2020 年 7 月 27 日
Oh no... it works... :)
I just realized that the two colors look very very similar in Matlab!!!!
I changed the RGB Code and realized it....
Thank you so much for your answer!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by