Matlab中的分组箱线图:通用功能 [英] Grouped boxplots in Matlab: a Generic function

查看:118
本文介绍了Matlab中的分组箱线图:通用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO中看到这篇很棒的文章后:

After seeing this great post in SO:

绘制分组箱形图matlab的最有效方法

我想知道是否可以创建一个这样的函数,但是要更通用一些,因为在我的应用程序中,我需要对不同情况下的不同算法进行几种分析,因此调整绘图代码非常繁琐.每种情况.

I was wondering if it is possible to create a function like that but a bit more generic, as in my application I need to make several analysis of different algorithms in different situations and it would be very tedious to tune the plotting code for each case.

我想要一些类似的情节:

I would like something generic for this kind of plots:

推荐答案

我编写了一个Matlab函数,为您(我)完成了此任务.

I coded a Matlab function that does that for you (me).

功能:

  • 在每个箱形图中,支持的数据量不同
  • 支持的每组任意数量的组和箱线图
  • 支持Xlabel和boxplotlabel
  • 自动选择颜色或用户指定的颜色

函数结果示例:

代码:

function multiple_boxplot(data,xlab,Mlab,colors)

% data is a cell matrix of MxL where in each element there is a array of N
% length. M is how many data for the same group, L, how many groups.
%
% Optional:
% xlab is a cell array of strings of length L with the names of each
% group
%
% Mlab is a cell array of strings of length M
%
% colors is a Mx4 matrix with normalized RGBA colors for each M.

% check that data is ok.
if ~iscell(data)
    error('Input data is not even a cell array!');
end

% Get sizes
M=size(data,2);
L=size(data,1);
if nargin>=4
    if size(colors,2)~=M
        error('Wrong amount of colors!');
    end
end
if nargin>=2
    if length(xlab)~=L
        error('Wrong amount of X labels given');
    end
end

% Calculate the positions of the boxes
positions=1:0.25:M*L*0.25+1+0.25*L;
positions(1:M+1:end)=[];

% Extract data and label it in the group correctly
x=[];
group=[];
for ii=1:L
    for jj=1:M
        aux=data{ii,jj};
        x=vertcat(x,aux(:));
        group=vertcat(group,ones(size(aux(:)))*jj+(ii-1)*M);
    end
end
% Plot it
boxplot(x,group, 'positions', positions);

% Set the Xlabels
aux=reshape(positions,M,[]);
labelpos = sum(aux,1)./M;

set(gca,'xtick',labelpos)
if nargin>=2
    set(gca,'xticklabel',xlab);
else
    idx=1:L;
    set(gca,'xticklabel',strsplit(num2str(idx),' '));
end


% Get some colors
if nargin>=4
    cmap=colors;
else
    cmap = hsv(M);
    cmap=vertcat(cmap,ones(1,M)*0.5);
end
color=repmat(cmap, 1, L);

% Apply colors
h = findobj(gca,'Tag','Box');
for jj=1:length(h)
   patch(get(h(jj),'XData'),get(h(jj),'YData'),color(1:3,jj)','FaceAlpha',color(4,jj));
end

if nargin>=3
    legend(fliplr(Mlab));
end
end

简单的例子:

clear;clc;
% Create example data
A=rand(100,10);
B=rand(200,10);
C=rand(150,10);

% prepare data
data=cell(10,3);
for ii=1:size(data,1)
    Ac{ii}=A(:,ii);
    Bc{ii}=B(:,ii);
    Cc{ii}=C(:,ii);
end
data=vertcat(Ac,Bc,Cc);

xlab={'Hey','this','works','pretty','nicely.','And','it','has','colors','!!!!'};
col=[102,255,255, 200; 
    51,153,255, 200;
    0, 0, 255, 200];
col=col/255;

multiple_boxplot(data',xlab,{'A', 'B', 'C'},col')
title('Here it is!')

Mathworks文件交换文件可以在这里找到: http://www.mathworks.com/matlabcentral/fileexchange/47233- multi-boxplot-m

Mathworks file exchange file can be found here: http://www.mathworks.com/matlabcentral/fileexchange/47233-multiple-boxplot-m

这篇关于Matlab中的分组箱线图:通用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆