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

조회 수: 7 (최근 30일)
Raki Kawama
Raki Kawama 2022년 4월 6일
댓글: Raki Kawama 2022년 4월 13일
現在、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]);

답변 (1개)

Hernia Baby
Hernia Baby 2022년 4월 7일
以下の回答を少し変更しています
% 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
Hernia Baby
Hernia Baby 2022년 4월 10일
findobj を使用してください
clear,clc;
% Generate random data
X = rand(10);
% Create a new figure and draw a box plot
figure;
boxplot(X,'Symbol','o','Colors',[.6 .6 .6]);
ここで findobj で外れ値のみを抜き出します
偶数は黒、奇数は赤にしています
h = findobj(gcf,'tag','Outliers');
for ii = 1:length(h)
if mod(ii,2) == 0;
h(ii).MarkerEdgeColor = 'k';
h(ii).MarkerFaceColor = 'k';
else
h(ii).MarkerEdgeColor = 'r';
h(ii).MarkerFaceColor = 'r';
end
end
平均値を付け足します
% Overlay the mean as green diamonds
hold on
plot(mean(X), '*b')
hold off
Raki Kawama
Raki Kawama 2022년 4월 13일
無事に変更できました。迅速なご回答ありがとうございました!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!