Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 0445b609 authored by Nicolas Bellot's avatar Nicolas Bellot Committed by hhakim
Browse files

bplot demo

parent 9b73ea08
Branches
Tags
No related merge requests found
% function forLegend = bplot(X,varargin)
% This function will create a nice boxplot from a set of data. You don't
% need any toolboxes. It will make one boxplot at a time.
% need any toolboxes.
%
% bplot(D) will create a boxplot of data D, no fuss.
%
......@@ -64,11 +64,9 @@
% points around the other axis so that you can see exactly how many are
% there.
%
%% Examples
% Note that this function will only plot one boxplot at a time.
%
% bplot(randn(1,30))
% bplot(randn(1,30),'color','black');
% % Examples:
% bplot(randn(30,3),'outliers')
% bplot(randn(30,3),'color','black');
% ----
% X = round(randn(30,4)*5)/5; % random, with some duplicates
% T = bplot(X,'points');
......@@ -83,12 +81,40 @@
% Jonathan Lansey 2013, %
% questions to Lansey at gmail.com %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% LICENSE %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2015, Jonathan C. Lansey
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%%
function forLegend = bplot(x,varargin)
function [forLegend, boxEdge, wisEdge] = bplot(x,varargin)
%% save the initial hold state of the figure.
hold_state = ishold;
if ~hold_state
clf;
cla;
end
%%
if size(x,1)>1 && size(x,2)>1 % great, you want to plot a bunch.
......@@ -109,6 +135,9 @@ if size(x,1)>1 && size(x,2)>1 % great, you want to plot a bunch.
end
end
if ~hold_state
hold off;
end
return;
end
......@@ -125,6 +154,13 @@ else % not text arguments, not even separate 'x' argument
y=1;
justOneInputFlag=1;
end
%% check that there is at least some data
if isempty(x)
warning('you asked for no data, so no data is what you plot.');
forLegend = '';
return;
end
%%
if length(y)>1
warning('The location can only be a scalar, it has been set to ''1''');
......@@ -145,6 +181,9 @@ meanFlag = 1;
specialWidthFlag = 0; % this flag will determine whether the bar width is
% automatically set as a proportion of the axis width
toScale = 0; % this flag is to scale the jitter function in case the
% histogram function is calling it
if justOneInputFlag
if length(x)<400
outlierFlag = 1;
......@@ -205,6 +244,9 @@ while k <= length(varargin)
widthFlag = 1;
case {'nomean'}
meanFlag=0;
case {'toscale','histmode','hist'}
toScale = 1; % scale away folks!
% case {'mode','modes'}
% modeFlag = 1;
% case {'text','alltext','t'} % ?????
......@@ -232,7 +274,7 @@ if ~widthFlag % if the user didn't specify a specific width of the bar.
end
%% calculate the necessary values for the sizes of the box and whiskers
boxEdge = prctile(x,[percentileNum 100-percentileNum]);
IQR=diff(boxEdge);
IQR=max(diff(boxEdge),eps); % in case IQR is zero, make it eps
if stdFlag
stdX = std(x);
wisEdge = [meanX-stdX meanX+stdX];
......@@ -246,7 +288,7 @@ hReg=[];
hReg2 = [];
if horizontalFlag
hReg2(end+1) = rectangle('Position',[boxEdge(1),y-barWidth/2,IQR,barWidth],'linewidth',linewidth,'EdgeColor',boxColor);
hReg2(end+1) = rectangle('Position',[boxEdge(1),y-barWidth/2,IQR,barWidth],'linewidth',linewidth,'EdgeColor',boxColor,'facecolor',[1 1 1]);
hold on;
hReg2(end+1) = plot([medianX medianX],[y-barWidth/2 y+barWidth/2],'color',meanColor,'linewidth',linewidth);
......@@ -260,7 +302,7 @@ if horizontalFlag
hReg2(end+1) = plot([wisEdge(1) wisEdge(1)],[y-barWidth/3 y+barWidth/3],'-','linewidth',linewidth,'color',wisColor);
hReg(end+1) = plot([wisEdge(2) wisEdge(2)],[y-barWidth/3 y+barWidth/3],'-','linewidth',linewidth,'color',wisColor);
else %
hReg2(end+1) = rectangle('Position',[y-barWidth/2,boxEdge(1),barWidth+1e-8,IQR+1e-8],'linewidth',linewidth,'EdgeColor',boxColor);
hReg2(end+1) = rectangle('Position',[y-barWidth/2,boxEdge(1),barWidth,IQR],'linewidth',linewidth,'EdgeColor',boxColor,'facecolor',[1 1 1]);
hold on;
hReg2(end+1) = plot([y-barWidth/2 y+barWidth/2],[medianX medianX],'color',meanColor,'linewidth',linewidth);
......@@ -285,32 +327,43 @@ if outlierFlag % but only if you want to
xx=x(I);
yy=I*0+y;
yy=yy(I);
yy = jitter(xx,yy);
yy = jitter(xx,yy,toScale);
if ~isempty(yy)
yy = jitter(xx,yy,toScale);
maxPointHeight = 2.5;
yy = (yy-y)*4+y;
yy = (yy-y)*(barWidth/maxPointHeight)/max([yy-y; barWidth/maxPointHeight])+y;
maxPointHeight = 2.5;
yy = (yy-y)*4+y;
yy = (yy-y)*(barWidth/maxPointHeight)/max([yy-y; barWidth/maxPointHeight])+y;
if ~isempty(xx)
if horizontalFlag
hReg2(6) = plot(xx,yy,'o','linewidth',linewidth,'color',wisColor);
else
hReg2(6) = plot(yy,xx,'o','linewidth',linewidth,'color',wisColor);
if ~isempty(xx)
if horizontalFlag
hReg2(6) = plot(xx,yy,'o','linewidth',linewidth,'color',wisColor);
else
hReg2(6) = plot(yy,xx,'o','linewidth',linewidth,'color',wisColor);
end
end
end
end
%% Remove the legend entries
% remove extras for all the items.
for ii=1:length(hReg)
set(get(get(hReg(ii),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); % Exclude line from legend
% Workaround for bug in 2015a leading '"no Annotation property on the
% Rectangle class" error
% set(get(get(hReg(ii),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); % Exclude line from legend
set(hReg(ii),'HandleVisibility','off')
end
% remove all remenants of legends
if forceNoLegend
for ii=1:length(hReg2)
set(get(get(hReg2(ii),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); % Exclude line from legend
% Workaround for bug in 2015a leading '"no Annotation property on the
% Rectangle class" error
% set(get(get(hReg2(ii),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); % Exclude line from legend
set(hReg2(ii),'HandleVisibility','off')
end
end
%% set the axis
% The axis is only messed with if you didn't pass a position value (because
% I figured you just wanted to make a quick plot without worry about much
......@@ -352,9 +405,13 @@ end
% in case two point appear at the same value, the jitter function will make
% them appear slightly separated from each other so you can see the real
% number of points at a given location.
function yy =jitter(xx,yy)
% tempY=yy(1);
tempY=1;
function yy =jitter(xx,yy,toScale)
if toScale
tempY=yy(1);
else
tempY=1;
end
for ii=unique(xx)';
I = xx==(ii);
fI = find(I)';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment