Grouping 2D contour plots into a 3D Figure
28 views (last 30 days)
Show older comments
Marshall Boden
on 30 Oct 2023
Commented: Cris LaPierre
on 30 Oct 2023
HI,
I have recently used the contour plots to map the velocity of a wind tunnel at different distances from the inlet. I am wondering how I can add these 2D contours (screenshot 1) into a 3D plotshowing multiple 2D contours on the same figure. I have the data stored in 5*5 Matrices (Screenshot 2), would i need to change this data into a different form for 3D plotting. The third screenshot is a example, similar to what I am hoping to achieve.
I am quite new to matlab so any help would be much appriciated.
Thanks
0 Comments
Accepted Answer
Cris LaPierre
on 30 Oct 2023
I would stack you 5x5 matrices together to create a volume V. I would then use meshgrid to create X, Y and Z arrays the same size as V. You might then be able to use contourslice or slice functions in MATLAB to create your desired figure.
x = [1945 2445 2945];
y = [-750 -375 0 375 750];
z = [230, 115, 0, -115, -230];
[X,Y,Z] = meshgrid(x,y,z);
V1=[3.649 5.354 5.208 5.441 3.456
3.264 5.725 5.679 5.728 2.667
2.931 5.7741 5.744 5.782 2.202
2.653 5.775 5.78 5.674 2.003
1.738 5.743 5.778 5.659 1.572];
V2 = V1 + rand(size(V1))*3-1.5;
V3 = V1 + rand(size(V1))*3-1.5;
V = cat(3,V1,V2,V3);
% Rearrange so columns = X, rows = Y, pages = Z
V = permute(V,[2,3,1]);
% contourslice
contourslice(X,Y,Z,V,[x],[],[])
xlabel('X Position')
ylabel('Y Position')
zlabel('Z Position')
colorbar
view(3)
grid on
% slice
slice(X,Y,Z,V,[x],[],[])
xlabel('X Position')
ylabel('Y Position')
zlabel('Z Position')
colorbar
The other option is to use hold on and add 3 contourf plots to your axes. (Adapted from here: https://www.mathworks.com/matlabcentral/answers/477979-plot-multiple-2d-contour-plots-in-one-3d-figure)
figure
[~,h] = contourf(squeeze(Y(:,1,:)),squeeze(Z(:,1,:)),squeeze(V(:,1,:)));
h.ContourZLevel = 1945;
hold on
[~,h]=contourf(squeeze(Y(:,2,:)),squeeze(Z(:,2,:)),squeeze(V(:,2,:)));
h.ContourZLevel = 2445;
[~,h]=contourf(squeeze(Y(:,3,:)),squeeze(Z(:,3,:)),squeeze(V(:,3,:)));
h.ContourZLevel = 2945;
hold off
xlabel('X Position')
ylabel('Y Position')
zlabel('Z Position')
colorbar
view(3)
4 Comments
Cris LaPierre
on 30 Oct 2023
One more thought. The plot you shared shows some deformation in Z (what corresponds to X here). You could use multiple surf plots. You'd have to upsample your data, and you'd have to figure out a way to scale the deformation to make it visible and meaningful. Here's a quick attempt.
% create data
x = [1945 2445 2945];
y = [-750 -375 0 375 750];
z = [230, 115, 0, -115, -230];
v1=[3.649 5.354 5.208 5.441 3.456
3.264 5.725 5.679 5.728 2.667
2.931 5.7741 5.744 5.782 2.202
2.653 5.775 5.78 5.674 2.003
1.738 5.743 5.778 5.659 1.572];
v2 = v1 + rand(size(v1));
v3 = v1 + rand(size(v1))*2-1;
% Upsample 5x5 grids using interp2
Y = linspace(min(y),max(y),25);
Z = linspace(min(z),max(z),25);
V1 = interp2(y,z,v1,Y,Z');
V2 = interp2(y,z,v2,Y,Z');
V3 = interp2(y,z,v3,Y,Z');
[Y,Z] = meshgrid(Y,Z);
% I opted to use the V1-3 to adjust X values to show displacement.
% Given the scale, I multiplied by 20 so that it was visible.
surf(x(1)+V1*50,Y,Z,V1)
hold on
surf(x(2)+V2*50,Y,Z,V2)
surf(x(3)+V3*50,Y,Z,V3)
hold off
xlabel('X Position')
ylabel('Y Position')
zlabel('Z Position')
colorbar
view(3)
grid on
More Answers (0)
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



