3D grouped bar-plots from 3D matrix

15 views (last 30 days)
I'm looking for a way to plot a 3D matrix (3x2x2) as a grouped 3D bar plot where each group has two different bars (according to the third dimension of the matrix). Please see the scratch below as a reference. Thanks

Accepted Answer

Mathieu NOE
Mathieu NOE on 6 Mar 2024
hello again
based on the comment / link above, this is what I can offer today
%generate somme dummy data
groups_x = 3;
rows_y = 2;
data = 2;
Y = rand(groups_x,rows_y,data);
% plot
figure(1);
hold on;
% define x spacing
x_spacing = 2;
% generate ticks / ticklabels
x_vector_ticks = (1:x_spacing:groups_x*x_spacing);
for ci = 1:length(x_vector_ticks)
XtickLabel{ci} = ['Group#' num2str(ci)];
end
y_vector_ticks = (1:rows_y);
for ci = 1:length(y_vector_ticks)
YtickLabel{ci} = ['Param#' num2str(ci)];
end
% main loop
for ci =1:groups_x
xval = 1+(ci-1)*x_spacing;
zval = squeeze(Y(ci,:,:));
h = bar3(zval,'grouped');
Xdat = get(h,'Xdata');
for ii=1:length(Xdat)
Xdat{ii}=Xdat{ii}+(xval-1)*ones(size(Xdat{ii}));
set(h(ii),'XData',Xdat{ii});
end
end
% set ticks / ticklabels
xticks(x_vector_ticks);
xticklabels(XtickLabel);
yticks(y_vector_ticks);
yticklabels(YtickLabel);
xlim([0 groups_x*x_spacing+1 ]);
view(15,20);
title('Grouped Style')
  3 Comments
Adam Danz
Adam Danz on 7 Mar 2024
You could probably adjust the DataAspectRatio to make the bars square at their base.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!