Code covered by the BSD License  

Highlights from
Simple bar plot with errorbars in 3D

image thumbnail
from Simple bar plot with errorbars in 3D by Sung Soo Kim
Simple bar plot with errorbars in 3D.

h=barError3d(data,upperErrorSize,lowerErrorSize,faceColor,x,y,width,edgeColor,errorBarWidth,errorColor)
function h=barError3d(data,upperErrorSize,lowerErrorSize,faceColor,x,y,width,edgeColor,errorBarWidth,errorColor)
% You can use this function in two ways. First one is simple.
%
% >> figure;
% >> data = cool(7);
% >> upperErrorSize = 0.1*ones(size(data));
% >> lowerErrorSize = [];
% >> faceColor = {'y','b','r';
%                 'y','b','r';
%                 'y','b','r';
%                 'y','b','r';
%                 'y','b','r';
%                 'y','b','r';
%                 'y','b','r'};
% >> h=barError3d(data,upperErrorSize,lowerErrorSize,faceColor);
%
% Second usage is to define all parameters. In this case, all parameters
% should have the same dimension, except upperErrorSize and lowerErrroSize
% which can be empty.
%
% >> figure;
% >> x=[0.5:1:size(data,1)-0.5]';
% >> x=repmat(x,1,size(data,2));
% >> y=1:size(data,2);
% >> y=repmat(y,size(data,1),1);
% >> width = 0.5*ones(size(data));
% >> a={[0,1,1]};
% >> edgeColor=repmat(a,size(data));
% >> errorBarWidth=0.8*ones(size(data));
% >> a={'m'};
% >> errorColor=repmat(a,size(data));
% >> h=barError3d(data,upperErrorSize,lowerErrorSize,faceColor, ...
%               x,y,width,edgeColor,errorBarWidth,errorColor);
%
% See <a href="matlab: web([docroot '/techdoc/ref/patch_props.html'])">Patch Properties</a> to modify rectangles, and 
%    see <a href="matlab: web([docroot '/techdoc/ref/line_props.html'])">Line Properties</a> to modify errorbars.
%
% Caution: Lower errorbars might not be shown properly depending on the
% view angle, because they are on the same plane as the rectangles.


if nargin==4
    x=[0.5:1:size(data,1)-0.5]';
    x=repmat(x,1,size(data,2));
    y=1:size(data,2);
    y=repmat(y,size(data,1),1);
    width = 0.85*ones(size(data));
    a = {'k'};
    edgeColor=repmat(a,size(data));
    errorBarWidth=0.5*ones(size(data));
    errorColor=edgeColor;
elseif nargin~=10
    error('Wrong number of input arguments');
end
h_patch=-1*ones(size(data));
h_errorline=-1*ones(size(data));
for i=1:numel(data)
    if ~isempty(upperErrorSize) && ~isempty(lowerErrorSize)
        [h_patch(i), h_errorline(i)] = makeEachBarError(x(i),y(i),data(i),upperErrorSize(i),lowerErrorSize(i), ...
            width(i),faceColor{i},edgeColor{i},errorBarWidth(i),errorColor{i});
    elseif ~isempty(upperErrorSize) && isempty(lowerErrorSize)
        [h_patch(i), h_errorline(i)] = makeEachBarError(x(i),y(i),data(i),upperErrorSize(i),[], ...
            width(i),faceColor{i},edgeColor{i},errorBarWidth(i),errorColor{i});
    elseif isempty(upperErrorSize) && isempty(lowerErrorSize)
        [h_patch(i), h_errorline(i)] = makeEachBarError(x(i),y(i),data(i),[],[], ...
            width(i),faceColor{i},edgeColor{i},errorBarWidth(i),errorColor{i});
    else
        error('lowerErrorSize cannot be used alone.');
    end
end
h.barPatches = h_patch;
h.errorLines = h_errorline;
view([1,-1,1]);
end


function [h_patch, h_errorline] = makeEachBarError(x,y,height,upperErrorSize,lowerErrorSize,width,faceColor,edgeColor,errorBarWidth,errorColor)

h_patch = makeBarPatch(x,y,height,upperErrorSize,lowerErrorSize,width,faceColor,edgeColor,errorBarWidth,errorColor);
if ~isempty(upperErrorSize)
    h_errorline = makeErrorLine(x,y,height,upperErrorSize,lowerErrorSize,width,faceColor,edgeColor,errorBarWidth,errorColor);
else
    h_errorline = -1;
end
end


function h_patch = makeBarPatch(x,y,height,upperErrorSize,lowerErrorSize,width,faceColor,edgeColor,errorBarWidth,errorColor)
xc=[x-width/2, x-width/2,   x+width/2,   x+width/2];
yc=[y,             y     ,  y      ,          y   ];
zc=[0,             height,  height,           0   ];

h_patch=patch(xc,yc,zc,faceColor);
set(h_patch,'EdgeColor',edgeColor);
end


function h_errorline = makeErrorLine(x,y,height,upperErrorSize,lowerErrorSize,width,faceColor,edgeColor,errorBarWidth,errorColor)
if isempty(lowerErrorSize)
    xc=[x, x, x-errorBarWidth/2, x+errorBarWidth/2 ];
    yc=[y, y, y,                    y  ];
    zc=[height, height+upperErrorSize, height+upperErrorSize, height+upperErrorSize  ];
else
    xc=[x+errorBarWidth/2, x-errorBarWidth/2,       x, x, x-errorBarWidth/2, x+errorBarWidth/2 ];
    yc=[y,y, y,y,y,y ];
    zc=[height-lowerErrorSize,height-lowerErrorSize,height-lowerErrorSize, height+upperErrorSize, height+upperErrorSize, height+upperErrorSize  ];
%     xc=[x+errorBarWidth/2, x-errorBarWidth/2,       x,x,x,x,x,x, x, x-errorBarWidth/2, x+errorBarWidth/2 ];
%     yc=[y,y, y, y+0.00000001, y+0.00000001,                    y-0.00000001 ,y-0.00000001,y,y,y,y ];
%     zc=[height-lowerErrorSize,height-lowerErrorSize,height-lowerErrorSize, height-lowerErrorSize,height+upperErrorSize, height+upperErrorSize,height-lowerErrorSize,height-lowerErrorSize, height+upperErrorSize, height+upperErrorSize, height+upperErrorSize  ];
end
h_errorline = line(xc,yc,zc);
set(h_errorline,'color',errorColor);
end

Contact us at files@mathworks.com