Display three 2D images into 3D space

22 views (last 30 days)
Tsuwei Tan on 30 Jul 2021
Edited: Adam Danz on 31 Jul 2021
disp_data.m
which will generate three 2D images (like "Picture 1.jpg" as attaced) from
data.mat
Please guide me how to change the aspect ratio (combine three 2D images on the same X-Y-Z axes) so basically I can make "Picture 1.jpg" to look like the "Example_pic.jpg" ?
I tried the view angle way but failed. Or I have a create and load one of the 2D images to rotate it and then combine?

Adam Danz on 31 Jul 2021
Edited: Adam Danz on 31 Jul 2021
pcolor does not support 3D coordinates so you'll have to use surf with flattened values along one of the dimensions for each plane. Usually the XY plane is at the minimum Z value of the data, the XZ plane is at the max Y value of the data, and the YZ plane is at the max X value of the data.
It's similar to the last example in this answer using marginal 2D histograms.
Judging by the axis labels, it looks like The Cs/Ps plot will be along the XY plane (the floor), H(m) will be along the Z plane so the Ps/H(m) plot will be on the XZ plane and the Cs/H(m) plot will be on the YZ plane or vise versa.
You can easily switch the planes by adjusting the logic demonstracted in the solution below.
The first block of code generates data and was copied and cleaned up from your m-file by removing comments but I haven't looked deeply into it nor have I changed any code. The warnings that appear suggest potentially suboptimal coding.
The second block of code is what you'll want to focus on. I have not judged whether the data are all oriented correctly -- see the second figure below to confirm the projections.
Generate data
% Copied and cleaned up a bit from OP's m-file.
Population_gen_cum = data.Population_gen_cum;
Score_gen_cum = data.Score_gen_cum;
[Min, index]=min(Score_gen_cum);
H_min=Population_gen_cum(index,1)/10;
cs_min=Population_gen_cum(index,2);
rho_s_min=Population_gen_cum(index,3)/100;
cb_min=Population_gen_cum(index,4);
rho_b_min=Population_gen_cum(index,5)/100;
D_min=Population_gen_cum(index,6)/10;
err_bnd=1; % This is 10_25_40Hz.
%% Create table
indx=Score_gen_cum<=(Min+err_bnd);
H=Population_gen_cum(indx,1)/10;
cs=Population_gen_cum(indx,2);
rho_s=Population_gen_cum(indx,3)/100;
cb=Population_gen_cum(indx,4);
rho_b=Population_gen_cum(indx,5)/100;
D=Population_gen_cum(indx,6)/10;
HH = [min(H); mean(H); max(H)];
css = [min(cs); mean(cs); max(cs)];
rhoss= [min(rho_s); mean(rho_s); max(rho_s)];
cbb = [min(cb); mean(cb); max(cb)];
rhobb= [min(rho_b); mean(rho_b); max(rho_b)];
DD = [min(D); mean(D); max(D)];
%% 3D Picture
Z=Score_gen_cum(indx);
%%
H_xy = linspace(min(H),max(H),1000) ;
cs_xy = linspace(min(cs),max(cs),1000) ;
rhos_xy = linspace(min(rho_s),max(rho_s),1000) ;
[H_X,cs_Y] = meshgrid(H_xy,cs_xy) ;
H_cs_Z = griddata(H,cs,Z,H_X,cs_Y) ;
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Produce plots
figure()
% Plot XY plane
[cs_X,rhos_Y] = meshgrid(cs_xy,rhos_xy) ;
cs_rhos_Z = griddata(cs,rho_s,Z,cs_X,rhos_Y) ;
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
minZ = min(H_X(:)); % Z Coordinates of the XY plane
surf(cs_X, rhos_Y, minZ*ones(size(cs_rhos_Z)), cs_rhos_Z)
hold on
% Add XZ plane where x-axis is C_s(/s)
maxY = max(rhos_Y(:)); % Y Coordinate of XZ plane
surf(cs_Y, maxY*ones(size(H_cs_Z)), H_X, H_cs_Z); % X-Z values switched to match 3D axes
% Add YZ plane where y-axis is P_s
[H_X,rhos_Y] = meshgrid(H_xy,rhos_xy) ;
H_rhos_Z = griddata(H,rho_s,Z,H_X,rhos_Y) ;
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
maxX = max(cs_X(:)); % X Coordinate of YZ plane
surf(maxX*ones(size(H_rhos_Z)), rhos_Y, H_X, H_rhos_Z) % Y-Z values switched to match 3D axes
colormap jet;
caxis([Min Min+err_bnd])
colormap(flipud(jet))
% Keep surfaces as close to the axis limits
% as possible for easier axis tick referencing
axis tight
box on
xlabel('c_s (m/s)');
ylabel('\rho_s');
zlabel('H (m)')
Verifying results
You may want to look into whatever transformations you're doing because the XZ and YZ planes do not look like projections for the XY plane components. The figure below plots the 3D version of the XY plane and the XY,YZ,XZ surfaces of the same data. The XZ and YZ planes do not look like the XZ ,YZ planes in your code.
figure()
tiledlayout(2,2)
% Plot 3D view
nexttile
sh = surf(cs_X, rhos_Y, cs_rhos_Z, cs_rhos_Z);
colormap jet;
caxis([Min Min+err_bnd])
colormap(flipud(jet))
xlabel('c_s (m/s)');
ylabel('\rho_s');
zlabel('H (m)')
title('3D view')
ax = nexttile();
copyobj(sh,ax)
view(0,90)
xlabel('c_s (m/s)');
ylabel('\rho_s');
title('XY view')
ax = nexttile();
copyobj(sh,ax)
view(0,0)
xlabel('c_s (m/s)');
zlabel('H (m)')
title('XZ view')
ax = nexttile();
copyobj(sh,ax)
view(90,0)
zlabel('H (m)')
ylabel('\rho_s');
title('YZ view')
Adam Danz on 31 Jul 2021
Happy to help. I'll leave it up to you to careful inspect it to confirm that it describes the data correctly.

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by