それぞれのboxpl​otに平均値をplo​tする+それぞれのb​oxplotの外れ値​の色を変える

25 views (last 30 days)
Raki Kawama
Raki Kawama on 6 Apr 2022
Commented: Raki Kawama on 13 Apr 2022
現在、boxplotを作成しているのですがそれぞれのboxに平均値をplotしたいです(現在は、一つのboxに全ての平均値がplotされてしまいます)。
また、奇数列のbox、偶数列のboxの外れ値の色をそれぞれブラック・グレーに変更したいです。
現在は、添付の画像が得られています。以下、スクリプトです。
初歩的な質問になりますが、よろしくお願いします。
clear all
subplot(5,1,1)
dat = readtable('file.csv')
x1 = dat(dat.Protocol==1,:);
x1_D = x1(x1.Dominant==1,:);
x1_N = x1(x1.Dominant==2,:);
x2 = dat(dat.Protocol==2,:);
x2_D = x2(x2.Dominant==1,:);
x2_N = x2(x2.Dominant==2,:);
x3 = dat(dat.Protocol==3,:);
x3_D = x3(x3.Dominant==1,:);
x3_N = x3(x3.Dominant==2,:);
x4 = dat(dat.Protocol==4,:);
x4_D = x4(x4.Dominant==1,:);
x4_N = x4(x4.Dominant==2,:);
x5 = dat(dat.Protocol==5,:);
x5_D = x5(x5.Dominant==1,:);
x5_N = x5(x5.Dominant==2,:);
X = [x1_D; x1_N; x2_D; x2_N;...
x3_D; x3_N; x4_D; x4_N; x5_D; x5_N];
% two group variables
g1 = X.Protocol;
g2 = X.Dominant;
% create box plot with extra spacing between the g1 boxes
boxplot(X.A, [g1 g2],'factorgap', 2,'Colors','k','FactorSeparator',[1,1],'width',0.6,'symbol','+k');
a = get(get(gca,'children'),'children');
t = get(a,'tag');
box1 = a([21 31 41 51 61 71 81]); % Difine of each box
box2 = a([23 33 43 53 63 73 83]);
box3 = a([25 35 45 55 65 75 85]);
box4 = a([27 37 47 57 67 77 87]);
box5 = a([29 39 49 59 69 79 89]);
box6 = a([22 32 42 52 62 72 82]);
box7 = a([24 34 44 54 64 74 84]);
box8 = a([26 36 46 56 66 76 86]);
box9 = a([28 38 48 58 68 78 88]);
box10 = a([30 40 50 60 70 80 90]);
meanbox1 = mean(x1_D.A)
meanbox2 = mean(x1_N.A)
meanbox3 = mean(x2_D.A)
meanbox4 = mean(x2_N.A)
meanbox5 = mean(x3_D.A)
meanbox6 = mean(x3_N.A)
meanbox7 = mean(x4_D.A)
meanbox8 = mean(x4_N.A)
meanbox9 = mean(x5_D.A)
meanbox10 = mean(x5_N.A)
hold on
plot(meanbox1,'ok')
plot(meanbox2,'ok')
plot(meanbox3,'ok')
plot(meanbox4,'ok')
plot(meanbox5,'ok')
plot(meanbox6,'ok')
plot(meanbox7,'ok')
plot(meanbox8,'ok')
plot(meanbox9,'ok')
plot(meanbox10,'ok')
hold off
set(box1, 'Color', [0.6784 0.6784 0.6784]) %Change box color
set(box2, 'Color', [0.6784 0.6784 0.6784])
set(box3, 'Color', [0.6784 0.6784 0.6784])
set(box4, 'Color', [0.6784 0.6784 0.6784])
set(box5, 'Color', [0.6784 0.6784 0.6784])
set(box6, 'Color', [0.1490 0.1490 0.1490])
set(box7, 'Color', [0.1490 0.1490 0.1490])
set(box8, 'Color', [0.1490 0.1490 0.1490])
set(box9, 'Color', [0.1490 0.1490 0.1490])
set(box10, 'Color', [0.1490 0.1490 0.1490])
set(box1,'LineWidth',1.25) % Change line width
set(box2,'LineWidth',1.25)
set(box3,'LineWidth',1.25)
set(box4,'LineWidth',1.25)
set(box5,'LineWidth',1.25)
set(box6,'LineWidth',1.25)
set(box7,'LineWidth',1.25)
set(box8,'LineWidth',1.25)
set(box9,'LineWidth',1.25)
set(box10,'LineWidth',1.25)
set(gca,'Fontsize',[12])
set(gca,'FontWeight','bold')
set(gca,'FontName','Times New Roman')
set(gca,'TickDir','out')
set(gca,'XTickLabel',{' '})
ylim([0 200]) %Change a range of y axis
yticks([0 50 100 150 200 250 300 350 400]);

Answers (1)

Hernia Baby
Hernia Baby on 7 Apr 2022
以下の回答を少し変更しています
% Generate random data
X = rand(10);
Symbol optionから外れ値を変更します
% Create a new figure and draw a box plot
figure;
boxplot(X,'Symbol','ok','Colors',[.6 .6 .6])
% Overlay the mean as green diamonds
平均を打ち込みます
hold on
plot(mean(X), '*r')
hold off
9列目を見ると外れ値のマーカーが変わっていることがわかります
  3 Comments
Raki Kawama
Raki Kawama on 13 Apr 2022
無事に変更できました。迅速なご回答ありがとうございました!

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!