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

 채택된 답변

Mathieu NOE
Mathieu NOE 2024년 3월 6일

2 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 3월 4일

댓글:

2024년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by