# How to plot a histogram in a waterfall plot?

14 views (last 30 days)
Nicolai on 21 Aug 2020
Edited: Abdolkarim Mohammadi on 22 Aug 2020
Hello,
I'd like to plot multiple histograms in a waterfall-like diagram. Does somebody know a way to that?
Example:
I don't want to use a three-dimensional plot as bar3 would give me, as I want the bars to be plane as in the pictured linked above.
Any hints or links would be highly appreciated!
Thank you!
##### 2 CommentsShow 1 older commentHide 1 older comment
Nicolai on 22 Aug 2020

Adam Danz on 21 Aug 2020
Edited: Adam Danz on 21 Aug 2020
The bars will have to have a width value but you can make that value very small to appear as flat.
For bar3(Z), when Z is a matrix, there will be one colored group of bar for each column of Z and each group will be centered along the same x-value. This demo shows how to 1) change the space between the groups and 2) decrease the widths of each bar so they appear flat.
% Load built-in data and plot a typical bar3() plot.
clf()
h = bar3(count(1:11,:));
xlabel('X-AXIS')
ylabel('Y-AXIS')
zlabel('Z-AXIS')
% Define the desired x-coordintes for each group of bars
% (must have the same number of elements as 'h')
newX = [1, 5, 9];
% Or, use
% newX = linspace(1,100,numel(h));
% Define bar width in depth
barWidths = 0.2;
% Extend x-axis limit
xlim([min(newX)-barWidths, max(newX)+barWidths])
% Reposition each group of bars
for i = 1:numel(h)
currentVals = [min(h(i).XData,[],'all'), max(h(i).XData,[],'all')];
h(i).XData(h(i).XData == currentVals(1)) = newX(i)-(barWidths/2);
h(i).XData(h(i).XData == currentVals(2)) = newX(i)+(barWidths/2);
end
% Change x-ticks
ax = h(1).Parent;
originalLabels = ax.XTickLabel;
ax.XTick = newX;
ax.XTickLabel = originalLabels;
You can also turn off the black edge lines which will help hide the depth of each bar (not shown in the image below).
set(h, 'EdgeColor', 'none') Adam Danz on 22 Aug 2020
Edited: Adam Danz on 22 Aug 2020
The choice of axes is arbitrary. I just changed the axis labels below and the viewing angle.
The pdfs (or whatever those curves are) can be added using plot3() while remembering that the x and y axes are swapped. The curves below are gaussians with mean and std computed from bar heights. % Load built-in data and plot a typical bar3() plot.
clf()
rawdata = randn(100,4);
data = arrayfun(@(i){histcounts(rawdata(:,i),10)'}, 1:size(rawdata,2));
data = round([data{:}] .* [.5 .75 1 1.25]);
h = bar3(data,.90);
xlabel('Y-AXIS')
ylabel('X-AXIS')
zlabel('Z-AXIS')
view([-145, 15])
hold on
% Define the desired x-coordintes for each group of bars
% (must have the same number of elements as 'h')
newX = linspace(1,100,numel(h));
% Define bar width in depth
barWidths = 0.1;
% Extend x-axis limit
xlim([min(newX)-barWidths, max(newX)+barWidths])
% Define gaus func
gaus = @(x,mu,sig,amp)amp*exp(-(((x-mu).^2)/(2*sig.^2)));
xCurve = linspace(.5,size(data,1)+.5, 100);
% Reposition each group of bars
for i = 1:numel(h)
currentVals = [min(h(i).XData,[],'all'), max(h(i).XData,[],'all')];
h(i).XData(h(i).XData == currentVals(1)) = newX(i)-(barWidths/2);
h(i).XData(h(i).XData == currentVals(2)) = newX(i)+(barWidths/2);
% Add bar-fits (assuming normal dist)
[mu,sig] = normfit(repelem(1:size(data,1), data(:,i)));
yCurve = gaus(xCurve, mu, sig, max(data(:,i)));
plot3(repmat(newX(i), size(xCurve)),xCurve, yCurve, 'k-', 'LineWidth', 3)
end
% Change x-ticks
ax = h(1).Parent;
originalLabels = ax.XTickLabel;
ax.XTick = newX;
ax.XTickLabel = originalLabels;
set(h, 'EdgeColor', [.5 .5 .5])

Abdolkarim Mohammadi on 22 Aug 2020
Edited: Abdolkarim Mohammadi on 22 Aug 2020
I think there is no function to draw this in one command. However, it can be drawn using patch() and plot3(). Use the following code. You must have all of your data in one matrix. If there are different number of observations in each variable, you can pad shorter variables with NaNs.
%% create dataset
rng (0);
Data = 50 + randn(80,5) - 2*(1:5);
Data = vertcat (Data,[50,50,50,NaN,NaN]);
%% draw plot
figure ('Position', [748,86,560,545]);
% get number of variables (columns of Data)
NumVariables = size(Data,2);
% colors we cycle through
Colors = [
0 0.4470 0.7410
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
0.4940 0.1840 0.5560
0.4660 0.6740 0.1880
0.3010 0.7450 0.9330
0.6350 0.0780 0.1840
];
% define relative bar width
RelativeBarWidth = 0.9;
% a template for vertices
VerticesTemplate = [
0 1 0
1 1 0
1 1 1
0 1 1
];
hold ('on')
% for each variable
for i1 = 1:NumVariables
% ---- Bar plot -------------
% remove nans
CurrentData = Data(isfinite(Data(:,i1)),i1);
% calculate bin counts and bin edges
[BinCounts,BinEdges] = histcounts(CurrentData);
% calculate number of bins
NumBins = numel(BinCounts);
% create vertices
Vertices = zeros(4*NumBins,3);
for i2 = 1:NumBins
Temp = VerticesTemplate;
Temp([1,4],1) = BinEdges(i2) + (1-RelativeBarWidth)*(BinEdges(i2+1)-BinEdges(i2));
Temp([2,3],1) = BinEdges(i2+1) - (1-RelativeBarWidth)*(BinEdges(i2+1)-BinEdges(i2));
Temp(:,2) = i1;
Temp(:,3) = Temp(:,3) * BinCounts(i2);
Vertices((i2-1)*4+1:i2*4,:) = Temp;
end
% create faces
Faces = reshape(1:4*NumBins,[4,NumBins])';
% draw bar plot
patch ('Faces', Faces, 'Vertices', Vertices, ...
'FaceVertexCData', repmat(Colors(i1,:),[NumBins,1]), ...
'FaceColor', 'Flat', ...
'EdgeColor', [0.6,0.6,0.6]);
% ---- Normal distribution -------------
% define accuracy
NumPoints = 101;
% calculate statistics
VarMin = min (CurrentData);
VarMax = max (CurrentData);
VarMean = mean (CurrentData);
VarStd = std (CurrentData,1);
% create x and calculate z for the line
% LineX = linspace (VarMin,VarMax,NumPoints)'; % shorter, non-adjustable tails
LineX = linspace (VarMean-3.5*VarStd,VarMean+3.5*VarStd,NumPoints)'; % longer, adjustable tails
LineY = repmat(i1,[NumPoints,1]);
LineZ = pdf('Normal',LineX,VarMean,VarStd) * sum(BinCounts,2);
% draw the line
plot3 (LineX, LineY, LineZ, ...
'Color', 'k', ...
'LineWidth', 2);
end
hold ('off')
view ([-35,33]);
xlabel ('Bin center');
yticklabels (["Americans","Europs","Asia","Africa","Oceania"]);
zlabel ('Counts');
box ('on');
grid ('on');
ax = gca;
ax.Projection = 'Perspective'; dpb on 22 Aug 2020
+1