共计 7103 个字符,预计需要花费 18 分钟才能阅读完成。
参考并致谢
https://zhuanlan.zhihu.com/p/…
https://zhuanlan.zhihu.com/p/…
用 boxplot 或者 boxchart 来绘图。
(1)boxplot 比拟直观,是数据按列分排好后,定义各个分组的指标地位,色彩等,间接就能够绘制进去。
(2)boxchart 把矩阵拉成列向量,依据分成几组 (factor) 以及每组有几种成分 (能够用色彩) 来绘制。
疑难:
boxchart 绘制的话怎么获取每个分组中各子箱线图的横坐标??暂未解决
- boxplot 通过边框分组
% 须要分组的数据,size 11*4
group1 = [2.5971 3.3004 2.5300 1.4816;...
4.5358 4.3818 2.3258 1.6239; ...
3.9728 3.9066 2.5668 2.3065; ...
4.8788 3.7533 2.3690 1.6233; ...
4.0810 4.0487 2.3443 1.4953; ...
4.3254 3.1829 3.5859 2.6255; ...
3.2180 3.9221 2.9497 1.9923; ...
4.6776 3.8937 3.2284 2.2298; ...
3.3915 3.8000 3.7869 1.9339; ...
4.2484 4.1819 1.8148 1.8452; ...
4.8277 4.1940 2.9338 1.7708]
group2 = [4.5730 4.3172 3.1523 1.6662; ...
5.7332 4.9923 3.2012 2.1913; ...
4.5736 4.8696 3.3527 3.0092; ...
5.5416 4.6020 2.7714 2.0871; ...
5.3273 4.0853 3.2455 2.2208; ...
5.2967 5.4538 4.0582 3.1677; ...
4.0403 4.4890 3.4762 2.7582; ...
3.6837 5.6476 4.2450 2.8042; ...
5.0343 5.1616 5.1146 2.1429; ...
5.2017 4.6968 2.6056 2.2122; ...
5.9474 5.7000 3.9155 2.2271]
% 边框色彩
color1 = [46,114,188]/255;
color2 = [206,85,255]/255;
pos1 = 0.8:3:9.8;
pos2 = 1.4:3:10.4;
%box_1 = boxplot(group1,'positions',pos1,'Colors',color1,'Widths',0.2,'Notch','on','Symbol','o','OutlierSize',5)
box_1 = boxplot(group1,'positions',pos1,'Colors',color1,'Widths',0.4,'Symbol','o','OutlierSize',5)
set(box_1,'LineWidth',1.5);
hold on;
box_2 = boxplot(group2,'positions',pos2,'Colors',color2,'Widths',0.4,'Symbol','o','OutlierSize',5)
set(box_2,'LineWidth',1.5);
%box_2 = boxplot(group2,'positions',pos2,'Colors',color2,'Widths',0.2,'Notch','on','Symbol','o','OutlierSize',5)
set(gca,'XTick', (pos1+pos2)/2, 'XTickLabel', ["Group1", "Group2","Group3","Group4"],'Xlim',[0 12],'Ylim',[0 7]);
plot(pos1, mean(group1), '-*','Color',color1)
hold on;
plot(pos2, mean(group2), '-*','Color',color2)
hold off
leg = findobj(gca,'Tag', 'Box')
% m = []
% n = []
% findobj 先索引到的是最右侧的色彩分组,也就是说色彩是反过来的,findobj 返回的 box 的程序是从最右侧到最左侧,不分明能够取出一个 box 通过看 XData 和 YData 坐标来晓得
for i = 1:length(leg)
% m = [m, get(leg(i), 'XData')]
% n = [n, get(leg(i), 'YData')]
end
% m1 = reshape(m, [], length(leg))
% m2 = m1'
% n1 = reshape(n, [], length(leg))
% n2 = n1'% plot(m2(1,:),n2(1,:),'-o')
% patch(m2(1,:),n2(1,:),'r')
l = legend([leg(5),leg(1)], ["A_ratio","B_ratio"])
set(l,'Interpreter','none') % 避免下划线_被误解析,或者可用 strrep
ylabel('Ratio')
set(gca,'XGrid','off','YGrid','on', 'LineWidth', 2, 'Fontsize', 11)
% savefig(gcf,'boxplot_ratio.fig');
% print(gcf, '-dpdf', 'boxplot_ratio.pdf')
失去分组箱线图
- boxplot 通过填充色彩分组
% 须要分组的数据,size 11*4
group1 = [2.5971 3.3004 2.5300 1.4816;...
4.5358 4.3818 2.3258 1.6239; ...
3.9728 3.9066 2.5668 2.3065; ...
4.8788 3.7533 2.3690 1.6233; ...
4.0810 4.0487 2.3443 1.4953; ...
4.3254 3.1829 3.5859 2.6255; ...
3.2180 3.9221 2.9497 1.9923; ...
4.6776 3.8937 3.2284 2.2298; ...
3.3915 3.8000 3.7869 1.9339; ...
4.2484 4.1819 1.8148 1.8452; ...
4.8277 4.1940 2.9338 1.7708]
group2 = [4.5730 4.3172 3.1523 1.6662; ...
5.7332 4.9923 3.2012 2.1913; ...
4.5736 4.8696 3.3527 3.0092; ...
5.5416 4.6020 2.7714 2.0871; ...
5.3273 4.0853 3.2455 2.2208; ...
5.2967 5.4538 4.0582 3.1677; ...
4.0403 4.4890 3.4762 2.7582; ...
3.6837 5.6476 4.2450 2.8042; ...
5.0343 5.1616 5.1146 2.1429; ...
5.2017 4.6968 2.6056 2.2122; ...
5.9474 5.7000 3.9155 2.2271]
% 边框色彩,都一样,用彩色
edgecolor1 = [0,0,0];
edgecolor2 = [0,0,0];
% 箱线图框内填充色彩
filledcolor1 = [46,114,188]/255;
filledcolor2 = [206,85,255]/255;
% 留神这里和办法 1 的色彩填充程序是反过来,先 filledcolor2 再 filledcolor1, 因为前面代码 findobj 先索引到的是最右侧的色彩分组
filledcolor = [repmat(filledcolor2, size(group1,2), 1); repmat(filledcolor1, size(group2,2), 1)]
pos1 = 0.8:3:9.8;
pos2 = 1.4:3:10.4;
box_1 = boxplot(group1,'positions',pos1,'Colors',edgecolor1,'Widths',0.4,'Symbol','o','OutlierSize',5)
set(box_1,'LineWidth',1.5);
hold on;
box_2 = boxplot(group2,'positions',pos2,'Colors',edgecolor2,'Widths',0.4,'Symbol','o','OutlierSize',5)
set(box_2,'LineWidth',1.5);
set(gca,'XTick', (pos1+pos2)/2, 'XTickLabel', ["Group1", "Group2","Group3","Group4"],'Xlim',[0 12],'Ylim',[0 7]);
plot(pos1, mean(group1), '-*', 'Color', filledcolor1)
plot(pos2, mean(group2), '-*','Color',filledcolor2)
hold off
% 这里 findobj 返回的 box 的程序是从最右侧到最左侧,不分明能够取出一个 box 通过看 XData 和 YData 坐标来晓得
boxobj = findobj(gca, 'Tag', 'Box')
% m = []
% n = []
for i = 1:length(boxobj)
patch(get(boxobj(i), 'XData'), get(boxobj(i), 'YData'), filledcolor(i,:), 'FaceAlpha', 0.5)
% m = [m, get(boxobj(i), 'XData')]
% n = [n, get(boxobj(i), 'YData')]
end
% m1 = reshape(m,[],length(boxobj))
% m2 = m1'
% n1 = reshape(n,[],length(boxobj))
% n2 = n1'% plot(m2(1,:),n2(1,:),'-o')
% patch(m2(1,:),n2(1,:),'r')
% a = get(boxobj(1),'XData')
% b = get(boxobj(1),'YData')
% patch(a,b,'red')
leg = get(gca, 'Children')
l = legend([leg(1),leg(5)], ["A_ratio","B_ratio"])
set(l,'Interpreter','none') % 避免下划线_被误解析,或者可用 strrep
ylabel('Ratio')
set(gca,'XGrid','off','YGrid','on', 'LineWidth', 2, 'Fontsize', 11)
% savefig(gcf,'boxplot_ratio.fig');
% print(gcf, '-dpdf', 'boxplot_ratio.pdf') % 保留为 pdf 文件
失去分组箱线图
- boxchart
% 须要分组的数据,size 11*4
group1 = [2.5971 3.3004 2.5300 1.4816;...
4.5358 4.3818 2.3258 1.6239; ...
3.9728 3.9066 2.5668 2.3065; ...
4.8788 3.7533 2.3690 1.6233; ...
4.0810 4.0487 2.3443 1.4953; ...
4.3254 3.1829 3.5859 2.6255; ...
3.2180 3.9221 2.9497 1.9923; ...
4.6776 3.8937 3.2284 2.2298; ...
3.3915 3.8000 3.7869 1.9339; ...
4.2484 4.1819 1.8148 1.8452; ...
4.8277 4.1940 2.9338 1.7708]
group2 = [4.5730 4.3172 3.1523 1.6662; ...
5.7332 4.9923 3.2012 2.1913; ...
4.5736 4.8696 3.3527 3.0092; ...
5.5416 4.6020 2.7714 2.0871; ...
5.3273 4.0853 3.2455 2.2208; ...
5.2967 5.4538 4.0582 3.1677; ...
4.0403 4.4890 3.4762 2.7582; ...
3.6837 5.6476 4.2450 2.8042; ...
5.0343 5.1616 5.1146 2.1429; ...
5.2017 4.6968 2.6056 2.2122; ...
5.9474 5.7000 3.9155 2.2271]
datatab = table();
datatab1 = table();
datatab2 = table();
% datatab.data = [group1(:,1);group2(:,1);group1(:,2);group2(:,2);group1(:,3);group2(:,3);group1(:,4);group2(:,4)];
data = zeros(size(group1,1), 2*size(group1, 2))
data(:, 1:2:end) = group1
data(:, 2:2:end) = group2
datatab.data = reshape(data,[],1)
datatab1.data1 = reshape(group1,[],1)
datatab2.data2 = reshape(group2,[],1)
factors = 4;
factor = [ones(2*size(group1, 1),1); 2*ones(2*size(group1, 1),1); 3*ones(2*size(group1, 1),1); 4*ones(2*size(group1, 1),1)]
factor1 = [ones(size(group1, 1),1); 2*ones(size(group1, 1),1); 3*ones(size(group1, 1),1); 4*ones(size(group1, 1),1)]
factor2 = [ones(size(group2, 1),1); 2*ones(size(group2, 1),1); 3*ones(size(group2, 1),1); 4*ones(size(group2, 1),1)]
groupmark1 = repmat(["A_ratio"], size(group1, 1), 1)
groupmark2 = repmat(["B_ratio"], size(group2, 1), 1)
groupmark = repmat([groupmark1; groupmark2], factors, 1)
datatab.group = groupmark
% 调整分组之间的间距,就用一个常数比方 1.5 乘以 factor
datatab.factor = 1.5*factor
datatab1.factor1 = 1.5*factor1
datatab2.factor2 = 1.5*factor2
% 接下来还想计算分组箱线图各自的均值,须要确定各均值点的横坐标
% 每个分组顺次的横坐标地位
factor_vec = 1.5*unique(factor)
% 每个分组左右两个箱线图,相当于在每个分组的核心左右平移了,这里是 0.5
factor_vec1 = factor_vec - 0.5
factor_vec2 = factor_vec + 0.5
color1 = [46, 114, 188]/255;
color2 = [206, 85, 30]/255;
color = {color1; color2};
%color = {[0.5,0.5,0.5];[0,0,0]};
axesbox = boxchart(datatab.factor,datatab.data,'GroupByColor',datatab.group)
hold on
meanRatio_1 = groupsummary(datatab1.data1, datatab1.factor1, 'mean')
x_1 = (factor_vec+factor_vec1)/2
plot(x_1, meanRatio_1, '-*', "Color",color1)
meanRatio_2 = groupsummary(datatab2.data2, datatab2.factor2, 'mean')
%plot([1.75:1.5:6.25], meanCNR_S, '-*', "Color",color2)
x_2 = (factor_vec+factor_vec2)/2
plot(x_2, meanRatio_2, '-*', "Color",color2)
hold off
set(axesbox, {'BoxFaceColor'}, color,{'MarkerColor'},color, 'LineWidth',1.5)
set(gca, 'XTick', factor_vec, 'XTickLabel', ["Group1", "Group2","Group3","Group4"])
set(gca,'XGrid','off','YGrid','on', 'LineWidth', 2, 'Fontsize', 11)
l= legend(axesbox)
% 避免下划线_被误解析,或者可用 strrep
set(l,'Interpreter','none')
ylabel('Ratio')
% savefig(gcf,'boxchart_ratio.fig');
% print(gcf, '-dpdf', 'boxchart_ratio.pdf')
失去分组箱线图
正文完