Moving the contour plot in 3D coordinate

75 visualizzazioni (ultimi 30 giorni)
程 彭
程 彭 il 7 Nov 2024 alle 12:34
Modificato: Bruno Luong il 8 Nov 2024 alle 11:05
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
  2 Commenti
程 彭
程 彭 il 7 Nov 2024 alle 14:20
Hello and thanks for your comment. I tried your method but could not get it correctly.
There are two added contours, one is just a cirle and the other is more complex and plotted with contourf. I just want to shift those two contours along the x axis to from x = 0 to x = 127.5. However, it doesn't seem to have a property to set it.
Or make it simpler, is there a way I can display a contour in a cubic domain rather than a plane?
程 彭
程 彭 il 8 Nov 2024 alle 1:53
Thanks again for your kind help. It appears I needed a translational matrix to shift the location of the contour plot.
The code per Bruno's answer would look like
HG = hgtransform(ax);
Rx = makehgtform('xrotate',pi/2);
Ty = makehgtform('translate',[0 127.5 0]);
HG.Matrix = Ty*Rx;
contour(YY,XX,density2D,midrho,'edgecolor','black','Linewidth',1.0,'Parent',HG);
contourf(YY,XX,velocity2D,30,'edgecolor','none','Parent',HG);
Using these lines to replace my original code
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);
would solve my problem.

Accedi per commentare.

Risposta accettata

Bruno Luong
Bruno Luong il 7 Nov 2024 alle 18:07
Modificato: Bruno Luong il 7 Nov 2024 alle 18:40
Translation 15 unit in y after rotation
Z = peaks;
ax = axes(figure);
HG = hgtransform(ax);
Rx = makehgtform('xrotate',pi/2);
Ty = makehgtform('translate',[0 15 0]);
HG.Matrix = Ty*Rx; % Translation 15 unit in y after rotation
contourf(Z, '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')
  1 Commento
程 彭
程 彭 il 8 Nov 2024 alle 1:46
Thanks for the help, Bruno. It worked as I need.
BTW, ZLocation appears to be a new feature. I use R2020a so I could not test this method, but it could be useful for people having newer versions.

Accedi per commentare.

Più risposte (1)

Bruno Luong
Bruno Luong il 7 Nov 2024 alle 14:52
Modificato: Bruno Luong il 7 Nov 2024 alle 15:11
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 Commenti
Bruno Luong
Bruno Luong il 7 Nov 2024 alle 18:48
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')
Bruno Luong
Bruno Luong il 8 Nov 2024 alle 7:57
Modificato: Bruno Luong il 8 Nov 2024 alle 11:05
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')

Accedi per commentare.

Categorie

Scopri di più su Lighting, Transparency, and Shading in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by