How to plot a histogram in a waterfall plot?

조회 수: 29 (최근 30일)
Nicolai
Nicolai 2020년 8월 21일
편집: Abdolkarim Mohammadi 2020년 8월 22일
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
dpb
dpb 2020년 8월 22일
I was able to make a very crude similar-looking thing with waterfall by using the pdf line from a fitdist object and then a stairs-like plot by using the bar plot x,y values. But, the rendering of the face color was lousy and then when added the second pdf, the rendering went completely bonkers.
All in all, that isn't a fruitful way forward I think.
Unfortunately, altho one can put a histogram on the same axes; it's in the x-y plane instead of the x-z plane. AFAICT there's no way to rotate the axes so one would have to write the code to draw the plane at the right angle from basic projections.
This is the result after the addition I spoke of; the front one looked not too awful bad until added the other and the patching went away...
Don't have to have the colorbar, I just put that on there as I was messing w/ CData values to try to match a color...you can see the two forms in outline form aren't bad before trying the color rendition to simulate a histogram...I don't know if there's any way to solve that rendering problem or not...
Nicolai
Nicolai 2020년 8월 22일
Thank you very much for your answer!

댓글을 달려면 로그인하십시오.

답변 (2개)

Adam Danz
Adam Danz 2020년 8월 21일
편집: Adam Danz 2020년 8월 21일
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.
See inline comments for details.
% Load built-in data and plot a typical bar3() plot.
load count.dat
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')
  댓글 수: 4
dpb
dpb 2020년 8월 22일
But can you figure out how to add the pdf on top on that axis, Adam?
Or any way to change its orientation to run in the x-axis direction a la waterfall to create the overlaid look OP wants?
Adam Danz
Adam Danz 2020년 8월 22일
편집: Adam Danz 2020년 8월 22일
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
Abdolkarim Mohammadi 2020년 8월 22일
편집: Abdolkarim Mohammadi 2020년 8월 22일
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')
%% adjust axes
view ([-35,33]);
xlabel ('Bin center');
yticklabels (["Americans","Europs","Asia","Africa","Oceania"]);
zlabel ('Counts');
box ('on');
grid ('on');
ax = gca;
ax.Projection = 'Perspective';
  댓글 수: 6
Abdolkarim Mohammadi
Abdolkarim Mohammadi 2020년 8월 22일
편집: Abdolkarim Mohammadi 2020년 8월 22일
You are right. The short tails were based on min and max and looked a little weird. I replaced it with tails based on deviations from the mean. It is now closer to what OP posted.
dpb
dpb 2020년 8월 22일
+1

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by