Stacked vertical 3d plot sorting

9 次查看(过去 30 天)
Hello,
I need some support in the sorting the vertical stacked bar plot one behind the other based on file name
Along the z axis:
1st row should contain 2 events pertaining to X File
2nd row should contain 3 events pertaining to Y File
3rd row should contain 1 events pertaining to Z File
Basically the 2d plot shown below should be converted to 3d plot in the order X,Y,Z along the z-axis one behind the other
I went through the below link to understand the bar3 but couldn't manage to develop a code by myself. Any help with the coding would be greatly appreciated, thanks!
https://in.mathworks.com/help/matlab/ref/bar3.html
  2 个评论
Adam Danz
Adam Danz 2020-10-5
What did you try with bar3() and why didn't it work? I'm sure we could set things straight.
sukanya
sukanya 2020-10-5
Hi Adam,
I tried it a while back and don't have the code handy at the moment but I always got 3d view with single row instead of 3-rows like the example shown below when I tried bar3() and all the 3 files were overlaid on the top of the other in that single row...

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-10-6
编辑:Adam Danz 2020-10-6
bar3(__) styles and limitations
Matlab's bar3(data) produces a n*m grid of 3D bars for n rows and m columns of the input matrix.
The bar3(data,'stacked') syntax produces a single row of n 3D bars with m stacked segments for an n*m matrix.
bar3(__) cannot do both. It doesn't support a grid of 3D bars that are also stacked.
Custom stacked_bar(__) function
Mike Garrity's retired blog post walks you through a custom function that applies stacked bars in a 3D grid (also attached). The input is an n*m*p 3D array where each page (3rd dimension) defines a layer in the stack.
Here's an illustration I made to show how the inputs to bar3(__) and the custom stacked_bar(__) shape the 3D bars (code to reproduce the plots is attached - bar3Demo.m).
See also
Applying this to your data
Assuming your data are stored in a table,
T = table(["X";"X";"Y";"Y";"Y";"Z"],[1;2;2;1;2;1],[2;1;1;2;1;2],[3;3;4;4;3;3],[4;3;3;3;3;4],...
'VariableNames',["Group","A","B","C","D"])
T = 6x5 table
Group A B C D _____ _ _ _ _ "X" 1 2 3 4 "X" 2 1 3 3 "Y" 2 1 4 3 "Y" 1 2 4 3 "Y" 2 1 3 3 "Z" 1 2 3 4
If you want to use the custom stacked_bar function you just need to reshape your data. Your desired layout of the 3D stacked bar plot is a bit unclear but here's now you'd reshape it to produce what I think you're looking for.
% Organize the data: custom bar3(data,'stacked') plot
[groups, groupID] = findgroups(T.Group);
datastack = nan(size(T,2)-1, max(accumarray(groups,1)), numel(groupID));
for i = 1:numel(groupID)
temp = T{T.Group==groupID(i),2:end}';
datastack(1:size(temp,1), 1:size(temp,2), i) = temp;
end
datastack = permute(datastack,[3,1,2]);
datastack(:,:,1) =
1 2 3 4 % first x
2 1 4 3 % first y
1 2 3 4 % first z
datastack(:,:,2) =
2 1 3 3 % 2nd x
1 2 4 3 % 2nd y
NaN NaN NaN NaN
datastack(:,:,3) =
NaN NaN NaN NaN
2 1 3 3 % 3rd y
NaN NaN NaN NaN
Then you just have to send datastack into the stacked_bar3 function.
figure()
ax = axes;
stacked_bar3(datastack)
grid on
box on
ax.XTick = 1:size(datastack,2);
ax.XTickLabel = T.Properties.VariableNames(2:end);
ax.YTick = 1:size(datastack,1);
ax.YTickLabel = groupID;
  2 个评论
sukanya
sukanya 2020-10-6
Thanks Adam, the solution looks promising. It is A,B,C,D that have to be stacked one on the top of the other in vertical stack. X,Y,Z are the different files that should appear in place of A,B,C,D in the figure above. Instead of X, Y, Z...It should be number of events in each file:
1st row - X - 2 events (instead of row A in the figure above)
2nd row - Y - 3 events (instead of row B in the figure above)
3rd row - Z - 1 event (instead of row C in the figure above)
I can work on this with the information you have provided, thanks a lot!
Adam Danz
Adam Danz 2020-10-6
It's just a matter of reshaping your matrix into an n*m*p 3D array and use NaN values as fillers.
data(:,:,1) will be the first 2D layer in the stack (blue in my demo image).
data(:,:,2) will be the 2nd 2D layer in the stack (orange in my demo image).
data(:,:,3) will be the 3rd 2D layer in the stack (yellow in my demo image).
If you need any help with that, let me know.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by