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
2020-10-5
What did you try with bar3() and why didn't it work? I'm sure we could set things straight.
采纳的回答
Adam Danz
2020-10-6
编辑:Adam Danz
2020-10-6
bar3(__) styles and limitations
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
- MathWorks support team answer for a similar function that also produces a stacked 3D grid of bars.
- File exhange function 4dbarchart
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"])
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 个评论
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 Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
