3D grouped bar-plots from 3D matrix

4 Ansichten (letzte 30 Tage)
Michele
Michele am 4 Mär. 2024
Kommentiert: Adam Danz am 7 Mär. 2024
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

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 6 Mär. 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 Kommentare
Mathieu NOE
Mathieu NOE am 6 Mär. 2024
my pleasure !
Adam Danz
Adam Danz am 7 Mär. 2024
You could probably adjust the DataAspectRatio to make the bars square at their base.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by