Kindly guide on how to connect the median points of boxplot with line and how to obtain respective colors of boxplot in the legend. Please help. Added the pot for reference.
조회 수: 18 (최근 30일)
이전 댓글 표시
CODE:
A1 = [36 38 39]';
B1 = [1 5 10]';
C1 = [-17 -11 -2]';
group = [ ones(size(A1));
2 * ones(size(B1));
3 * ones(size(C1))];
h1=boxplot([A1; B1; C1],group,'colors','b','widths',0.25,'BoxStyle','filled')
lines = findobj(gcf, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'g');
hold on
a3 = [27 30 36]';
b3 = [-26 -16 1]';
c3 = [-64 -47 -18]';
group = [ ones(size(a3));
2 * ones(size(b3));
3 * ones(size(c3))];
% figure
h2=boxplot([a3; b3; c3],group,'colors','r','widths',0.25,'BoxStyle','filled')
set(gca,'XTickLabel',{'1','3','5'})
lines = findobj(gcf, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'y');
hold on
a5 = [-14 1 35]';
b5 = [-152 -104 -3]';
c5 = [-274 -195 -26]';
group = [ ones(size(a5));
2 * ones(size(b5));
3 * ones(size(c5))];
% figure
h3=boxplot([a5; b5; c5],group,'colors','g','widths',0.25,'BoxStyle','filled')
set(gca,'XTickLabel',{'1','3','5'})
lines = findobj(gcf, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'y');
legend(findobj(gca,'Tag','Box'),'a','b','c')
댓글 수: 0
채택된 답변
Adam Danz
2021년 5월 22일
There are multiple ways to do this. Since you're already finding the median line objects, you could extract the coordinates from those lines (demonstrated below).
Otherwise, you could compute the medians directly from each group.
A1 = [36 38 39]';
B1 = [1 5 10]';
C1 = [-17 -11 -2]';
group = repelem((1:3)',[numel(A1);numel(B1);numel(C1)]);
h1=boxplot([A1; B1; C1],group,'colors','b','widths',0.15,'BoxStyle','filled');
lines = findobj(gcf, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'g','LineWidth',2);
hold on
xMed = mean(vertcat(lines.XData),2); % n x 1
yMed = vertcat(lines.YData); % n x 2 (duplicate columns)
plot(xMed, yMed(:,1), 'ro-')
댓글 수: 4
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!