3D grouped bar-plots from 3D matrix

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

 Réponse acceptée

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 commentaires

Michele
Michele le 6 Mar 2024
Thanks a lot for the valid support. It works perfectly!
Mathieu NOE
Mathieu NOE le 6 Mar 2024
my pleasure !
Adam Danz
Adam Danz le 7 Mar 2024
You could probably adjust the DataAspectRatio to make the bars square at their base.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by