The ability to display negative data values on the negative side of the axis when creating a stacked bar graph using BAR or BARH functions is currently not available in MATLAB.
As a work around, consider the following code
function barExtended(varargin)
if nargin == 1
Y = varargin{1};
else
Y = varargin{2};
end
fh(1) = figure;
hP = bar(gca,Y.*(Y>0),'stacked');
ahP = gca;
fh(2) = figure;
hN = bar(Y.*(Y<0),'stacked');
if nargin == 1
set([hP,hN],'parent',ahP);
delete(fh(2));
else
set([hP,hN],'parent',varargin{1});
delete(fh);
end
end
Following is an illustrative example:
barExtended(rand(5)-rand(5))
As another possible workaround you can also use the function BARX attached with this solution.
Best Answer