【基础教程】基于Matlab画花式箱体图

【基础教程】基于Matlab画花式箱体图箱体图 Boxplot 是一种表示数据分布的方法 wiki boxplot 一个基本的箱体图从上到下分别表示最大值 上四分位 均值 下四分位 最小值

大家好,欢迎来到IT知识分享网。

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法  神经网络预测 雷达通信  无线传感器

信号处理 图像处理 路径规划 元胞自动机 无人机

⛄ 内容介绍

箱体图Boxplot是一种表示数据分布的方法(wiki:boxplot),一个基本的箱体图从上到下分别表示最大值,上四分位,均值,下四分位,最小值。有的箱体图中还会加入异常值等。

箱体图有以下几个优点:

  1. 可以直观明了地识别数据中的异常值

2. 利用箱体图可以判断数据的偏态和尾重

3. 利用箱体图可以比较不同批次的数据形状

⛄ 部分代码

% script to show different ways to use adjusted_boxplot

% with built in data sets

%%

clear

close all

load examgrades.mat

XTickLabels={‘test A’,’test B’,’test C’,’test D’,’test E’};

[~,ind]=sort(grades(:,1)); % sort all grades based on first test

grades=grades(ind,:); % this will auto shift outlyers from left(low) to right(high)

groupcount=size(grades,2);

colors=lines(groupcount);

cc=mat2cell(colors,ones(groupcount,1),3);

figure(4321);clf

subplot(2,2,1);hold on;title(‘whisker_boxplot’)

whisker_boxplot(1:groupcount,grades,cc);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

subplot(2,2,2);hold on;title(‘adjusted_boxplot’)

adjusted_boxplot(1:groupcount,grades,cc);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

subplot(2,2,3);hold on;title(sprintf(‘whisker_boxplot \nwith bubbles & saturation set to white’))

whisker_boxplot(1:groupcount,grades,cc,’bub’,1,’sat’,0);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

subplot(2,2,4);hold on;title(sprintf(‘adjusted_boxplot \nwith bubbles & saturation set to white’))

adjusted_boxplot(1:groupcount,grades,cc,’bub’,1,’sat’,0);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

%%

clear

load discrim.mat

XTickLabels=strtrim(mat2cell(categories,ones(size(categories,1),1),size(categories,2)))’;

namecount=size(names,1);

cc=strtrim(mat2cell(names,ones(namecount,1),43));

C=cellfun(@(x) strsplit(x,’, ‘),cc,’uni’,0);

simplestate=cellfun(@(x) upper(x{end}(1:2)),C,’uni’,0);

[ustate,order]=unique(sort(simplestate));

statecount=diff([order; length(simplestate)+1]);

bigstates=ustate(statecount>15); %find only states with >15 scores/cities

runthese=ismember(simplestate,bigstates);

xs=.4/3; %the best half width for a ‘group or catagory’ is .4 and there are 3 big states

width=xs*.85;% width of each box with a small gap between subgroups

sgxs=[-xs 0 xs]*2;% SubGroups X Shift (twice the half width)

cc={[0 0 1],[1 .5 0],[1 0 0]};% subgroups color (blue orange red)

figure(2314);clf

subplot(2,1,1);hold on;title(‘whisker_boxplot’)

for ii=1:length(bigstates)

    for jj=1:length(XTickLabels)

        ratings(ismember(simplestate,bigstates(ii)),jj);

        h1{ii}=whisker_boxplot(jj+sgxs(ii),ratings(ismember(simplestate,bigstates(ii)),jj), cc{ii},’width’,width);

    end

end

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

legend([h1{1}(1) h1{2}(1) h1{3}(1)],bigstates)

subplot(2,1,2);hold on;title(‘adjusted_boxplot’)

for ii=1:length(bigstates)

    for jj=1:length(XTickLabels)

        ratings(ismember(simplestate,bigstates(ii)),jj);

        h1{ii}=adjusted_boxplot(jj+sgxs(ii),ratings(ismember(simplestate,bigstates(ii)),jj), cc{ii},’width’,width);

    end

end

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

legend([h1{1}(1) h1{2}(1) h1{3}(1)],bigstates)

%%

clear

load hospital

namecount=size(hospital,1);

shiftxs=hospital.Age-min(hospital.Age);

shiftxs=shiftxs/max(shiftxs)*2-1; % set range for shifting each point to -1:1 based on age 

Sex=strtrim(mat2cell(char(hospital.Sex),ones(namecount,1),6));

male=strcmpi(Sex,’male’);

xs=.4/2; %the best half width for a ‘group or catagory’ is .4 and there are 2 subgroups (male/female)

width=xs*.85;% width of each box with a small gap between subgroups

%

% plot weight, then plot pressure(1), then pressure(2)

XTickLabels={‘weight’ ‘systolic’ ‘diastolic’};

figure(8134);clf;

subplot(2,2,1);hold on;title(‘whisker_boxplot’)

l1=whisker_boxplot(1-xs,hospital.Weight( male),[0 0 1],’shiftxs’,shiftxs*width,’width’,width);

l2=whisker_boxplot(1+xs,hospital.Weight(~male),[1 0 0],’shiftxs’,shiftxs*width,’width’,width);

whisker_boxplot(2-xs,hospital.BloodPressure( male,1),[0 0 1],’shiftxs’,shiftxs*width,’width’,width);

whisker_boxplot(2+xs,hospital.BloodPressure(~male,1),[1 0 0],’shiftxs’,shiftxs*width,’width’,width);

whisker_boxplot(3-xs,hospital.BloodPressure( male,2),[0 0 1],’shiftxs’,shiftxs*width,’width’,width);

whisker_boxplot(3+xs,hospital.BloodPressure(~male,2),[1 0 0],’shiftxs’,shiftxs*width,’width’,width);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

legend([l1(1) l2(1)],{‘male’,’female’});

subplot(2,2,2);hold on;title(‘adjusted_boxplot’)

adjusted_boxplot(1-xs,hospital.Weight( male),[0 0 1],’shiftxs’,shiftxs*width,’width’,width);

adjusted_boxplot(1+xs,hospital.Weight(~male),[1 0 0],’shiftxs’,shiftxs*width,’width’,width);

adjusted_boxplot(2-xs,hospital.BloodPressure( male,1),[0 0 1],’shiftxs’,shiftxs*width,’width’,width);

adjusted_boxplot(2+xs,hospital.BloodPressure(~male,1),[1 0 0],’shiftxs’,shiftxs*width,’width’,width);

adjusted_boxplot(3-xs,hospital.BloodPressure( male,2),[0 0 1],’shiftxs’,shiftxs*width,’width’,width);

adjusted_boxplot(3+xs,hospital.BloodPressure(~male,2),[1 0 0],’shiftxs’,shiftxs*width,’width’,width);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

subplot(2,2,3);hold on;title(‘whisker_boxplot, with bubbles & saturation set to white’)

whisker_boxplot(1-xs,hospital.Weight( male),[0 0 1],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

whisker_boxplot(1+xs,hospital.Weight(~male),[1 0 0],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

whisker_boxplot(2-xs,hospital.BloodPressure( male,1),[0 0 1],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

whisker_boxplot(2+xs,hospital.BloodPressure(~male,1),[1 0 0],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

whisker_boxplot(3-xs,hospital.BloodPressure( male,2),[0 0 1],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

whisker_boxplot(3+xs,hospital.BloodPressure(~male,2),[1 0 0],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

subplot(2,2,4);hold on;title(‘adjusted_boxplot, with bubbles & saturation set to white’)

adjusted_boxplot(1-xs,hospital.Weight( male),[0 0 1],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

adjusted_boxplot(1+xs,hospital.Weight(~male),[1 0 0],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

adjusted_boxplot(2-xs,hospital.BloodPressure( male,1),[0 0 1],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

adjusted_boxplot(2+xs,hospital.BloodPressure(~male,1),[1 0 0],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

adjusted_boxplot(3-xs,hospital.BloodPressure( male,2),[0 0 1],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

adjusted_boxplot(3+xs,hospital.BloodPressure(~male,2),[1 0 0],’shiftxs’,shiftxs*width,’width’,width,’bub’,1,’sat’,0);

set(gca,’XTick’,1:length(XTickLabels),’XTickLabels’,XTickLabels)

⛄ 运行结果

【基础教程】基于Matlab画花式箱体图

【基础教程】基于Matlab画花式箱体图

【基础教程】基于Matlab画花式箱体图

⛄ 参考文献

❤️ 关注我领取海量matlab电子书和数学建模资料

❤️部分理论引用网络文献,若有侵权联系博主删除

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/129368.html

(0)
上一篇 2025-08-24 19:26
下一篇 2025-08-24 19:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信