参考并致谢用boxplot或者boxchart来绘图。
https://zhuanlan.zhihu.com/p/...
https://zhuanlan.zhihu.com/p/...
(1)boxplot比较直观,是数据按列分排好后,定义各个分组的目标位置,颜色等,直接就可以绘制出来。
(2)boxchart把矩阵拉成列向量,根据分成几组(factor)以及每组有几种成分(可以用颜色)来绘制。
疑问:
boxchart绘制的话怎么获取每个分组中各子箱线图的横坐标??暂未解决
- boxplot通过边框分组
% 需要分组的数据,size 11*4
group1 = [2.59713.30042.53001.4816;
...
4.53584.38182.32581.6239;
...
3.97283.90662.56682.3065;
...
4.87883.75332.36901.6233;
...
4.08104.04872.34431.4953;
...
4.32543.18293.58592.6255;
...
3.21803.92212.94971.9923;
...
4.67763.89373.22842.2298;
...
3.39153.80003.78691.9339;
...
4.24844.18191.81481.8452;
...
4.82774.19402.93381.7708]
group2 = [4.57304.31723.15231.6662;
...
5.73324.99233.20122.1913;
...
4.57364.86963.35273.0092;
...
5.54164.60202.77142.0871;
...
5.32734.08533.24552.2208;
...
5.29675.45384.05823.1677;
...
4.04034.48903.47622.7582;
...
3.68375.64764.24502.8042;
...
5.03435.16165.11462.1429;
...
5.20174.69682.60562.2122;
...
5.94745.70003.91552.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.59713.30042.53001.4816;
...
4.53584.38182.32581.6239;
...
3.97283.90662.56682.3065;
...
4.87883.75332.36901.6233;
...
4.08104.04872.34431.4953;
...
4.32543.18293.58592.6255;
...
3.21803.92212.94971.9923;
...
4.67763.89373.22842.2298;
...
3.39153.80003.78691.9339;
...
4.24844.18191.81481.8452;
...
4.82774.19402.93381.7708]
group2 = [4.57304.31723.15231.6662;
...
5.73324.99233.20122.1913;
...
4.57364.86963.35273.0092;
...
5.54164.60202.77142.0871;
...
5.32734.08533.24552.2208;
...
5.29675.45384.05823.1677;
...
4.04034.48903.47622.7582;
...
3.68375.64764.24502.8042;
...
5.03435.16165.11462.1429;
...
5.20174.69682.60562.2122;
...
5.94745.70003.91552.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.59713.30042.53001.4816;
...
4.53584.38182.32581.6239;
...
3.97283.90662.56682.3065;
...
4.87883.75332.36901.6233;
...
4.08104.04872.34431.4953;
...
4.32543.18293.58592.6255;
...
3.21803.92212.94971.9923;
...
4.67763.89373.22842.2298;
...
3.39153.80003.78691.9339;
...
4.24844.18191.81481.8452;
...
4.82774.19402.93381.7708]
group2 = [4.57304.31723.15231.6662;
...
5.73324.99233.20122.1913;
...
4.57364.86963.35273.0092;
...
5.54164.60202.77142.0871;
...
5.32734.08533.24552.2208;
...
5.29675.45384.05823.1677;
...
4.04034.48903.47622.7582;
...
3.68375.64764.24502.8042;
...
5.03435.16165.11462.1429;
...
5.20174.69682.60562.2122;
...
5.94745.70003.91552.2271]datatab = table();
datatab1 = table();
datatab2 = table();
% datatab.data = https://www.it610.com/article/[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')
【Matlab几种分组箱线图画法】得到分组箱线图
文章图片
推荐阅读
- react项目的@路径配置
- 阿里考试的经历和心得
- 阿里云ACE认证含金量高不高(考试内容难不难?)
- Flink 通过 State Processor API 实现状态的读取和写入
- Flink 源码分析之 Client 解析流程分析
- Flink Collector Output 接口源码解析
- 软件测试|MySQL详细知识点总结 可以收藏啦
- 慕课头条(灵动岛色差好严重;马斯克称燃油车不保值;某人事岗招聘不要单身人士)
- 在线直播 | 业务人员也能在Power Platform上制作自己的APP啦