Moving the contour plot in 3D coordinate
Show older comments
Hello MATLAB community,
I am trying to make a plot of a 3D isosurface overlaying with a contour plot on a plane crossing the isosurface.
Here is my code and output results
clear all;
nx = 256; ny = 256; nz = 256; ratio = 2;
mx = nx/ratio; my = ny/ratio; mz = nz/ratio;
x = [0.5:ratio:nx-0.5]; y = [0.5:ratio:ny-0.5]; z = [0.5:ratio:nz-0.5];
xx = [0.5:1:nx-0.5]; yy = [0.5:1:ny-0.5];
rhol = 5.116; rhog = 2.027; midrho = 0.5*(rhol + rhog);
[X,Y,Z] = meshgrid(y,x,z); [XX,YY] = meshgrid(yy,xx);
filename1a = 'density3D00.dat';
filename1b = 'velocity3D00.dat';
filename2a = 'densityCut00.dat';
filename2b = 'velocityCut00.dat';
A1 = load(filename1a);
A2 = load(filename1b);
B1 = load(filename2a);
B2 = load(filename2b);
density3D = A1(:,1);
velocity3D = A2(:,1);
density2D = B1(:,1);
velocity2D = B2(:,1);
density3D = reshape(density3D,mx,my,mz);
velocity3D = reshape(velocity3D,mx,my,mz);
density2D = reshape(density2D,nx,ny);
velocity2D = reshape(velocity2D,nx,ny);
figure(1);
isosurface(X,Y,Z,density3D,midrho,velocity3D);
colormap(othercolor('BuDRd_12'));
c = colorbar();
c.FontName = 'Times';
c.FontSize = 15;
c.Location = 'southoutside';
originalColorLimits = caxis(gca);
hold on;
ax = gca;
HG = hgtransform(ax);
contour(YY,XX,density2D,midrho,'edgecolor','black','Linewidth',1.0,'Parent',HG);
contourf(YY,XX,velocity2D,30,'edgecolor','none','Parent',HG);
HG.Matrix = makehgtform('xrotate',pi/2);
caxis(gca, originalColorLimits);
axis([0 256 0 256 0 256]);
set(gca,'box','on')
ax = gca;
ax.BoxStyle = 'full';
brighten(0.0);
daspect([1 1 1]);
view([-20 160])
xlabel('$y$','fontname','Times','Fontsize',20,'interpreter','Latex');
ylabel('$x$','fontname','Times','Fontsize',20,'interpreter','Latex');
zlabel('$z$','fontname','Times','Fontsize',20,'interpreter','Latex');
aaa = get(gca,'XTick');
set(gca,'XTick',aaa,'fontname','Times','fontsize',20)
bbb = get(gca,'YTick');
set(gca,'YTick',bbb,'fontname','Times','fontsize',20)
set(gcf, 'Position', [100, 100, 800, 800])

My question is, this contour is supposed to locate on the plane of x = 127.5, rather than x = 0 plane, how should I change the location?
Also, although I managed to overlay the contour plot into the plot of isosurface with 'makehgtform', I wonder if it is possible to add two other contour plots, say, one crossing the y = 127.5 plane and the other crossing z = 127.5 plane.
If it is possible, could you please show me the example code to do that, since I am not familiar with 'makehgtform'.
Thanks a lot!
Cheng
Accepted Answer
More Answers (1)
Bruno Luong
on 7 Nov 2024
Edited: Bruno Luong
on 7 Nov 2024
May be changing ZLocation property is what you need
Z = peaks();
surf(Z);
hold on
contour(Z,'Linewidth',2,'ZLocation',26);
contourf(Z,'ZLocation',13,'FaceAlpha',0.5);
view(3)
2 Comments
Move contours down on z is equivalent to move forward in y after rotation
Z = peaks;
ax = axes(figure);
HG = hgtransform(ax);
Rx = makehgtform('xrotate',pi/2);
HG.Matrix = Rx; %
contourf(Z, 'ZLocation',-15, 'parent', HG);
axis(ax,[0 40 0 40 0 40])
axis(ax,'equal')
view(ax,[-20 160])
xlabel(ax,'x')
ylabel(ax,'y')
zlabel(ax,'z')
If your MATLAB uses HG2 graphic (since R2014B) and does not support Zlocation property, here is a workaround
Z = peaks;
ax = axes(figure);
HG = hgtransform(ax);
Rx = makehgtform('xrotate',pi/2);
HG.Matrix = Rx; %
[~,hc] = contourf(Z, 'parent', HG);
hc.ContourZLevel = -15; % <= undocumented property
axis(ax,[0 40 0 40 0 40])
axis(ax,'equal')
view(ax,[-20 160])
xlabel(ax,'x')
ylabel(ax,'y')
zlabel(ax,'z')
Categories
Find more on Volume Visualization 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!


