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

✅作者简介:热爱科研的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电子书和数学建模资料

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值