Cross section of a plot (surface, Imagesc) using an arbitrary line

52 visualizaciones (últimos 30 días)
I have a plot of surface(x,y,z) (or an imagesc) and want to draw an arbitrary line to get the z information following that path.
So I have two questions:
First, how to draw a path that coincides with my x,y grid? Let's say I want a semicircular path, but it can be whichever.
And second, how do I get the values that correspond to the Z information that follows that said path?
I am not sure if what I am asking is too complex to answer, but I would love to hear your comments,
Thanks in advance.

Respuesta aceptada

Cameron
Cameron el 9 de Mzo. de 2023
Editada: Cameron el 9 de Mzo. de 2023
You can do this by interpolating. Depending on your requirements, you can use cubic, nearest neighbor, makima, linear, or spline interpolation. Here's an example:
[X,Y] = meshgrid(-5:.5:5); %some x and y data
Z = Y.*sin(X) - X.*cos(Y); %some z data
s = surf(X,Y,Z,'FaceAlpha',0.5); %surface plot
s.Parent.View = [0 90]; %view the plot
x1 = -2:0.1:2; %some x path
y1 = 3*sin(2*x1); %some y path
InterpStyle = 'spline'; %you can choose cubic, nearest, makima, linear, or spline
z1 = interp2(X,Y,Z,x1,y1,'cubic',0); %your interpolated z values
hold on
plot3(x1,y1,z1,'-k','LineWidth',2) %show the new data
hold off
s.Parent.View = [12 25]; %you can change the view to see what the black line looks like in 3D
  2 comentarios
Cameron
Cameron el 9 de Mzo. de 2023
I really liked the solution by @Image Analyst so I'll paste a version of my code that uses some of his work.
[X,Y] = meshgrid(-5:.5:5); %some x and y data
Z = Y.*sin(X) - X.*cos(Y); %some z data
s = surf(X,Y,Z,'FaceAlpha',0.5); %surface plot
s.Parent.View = [0 90]; %view the plot
m = drawfreehand;
x1 = m.Position(:,1); %some x path you drew
y1 = m.Position(:,2); %some y path you drew
InterpStyle = 'spline'; %you can choose cubic, nearest, makima, linear, or spline
z1 = interp2(X,Y,Z,x1,y1,'cubic',0); %your interpolated z values
hold on
plot3(x1,y1,z1,'-k','LineWidth',2) %show the new data
hold off
delete(m)
Contoso Donoso
Contoso Donoso el 10 de Mzo. de 2023
This makes exaclty what I was needing, i just needed to change make few changes, firts to use view(2) then change plot3 line into plot3(x1,z1,'-k','LineWidth',2). Perfect, thanks a lot! <3

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 9 de Mzo. de 2023
That's exactly what my attached demo does. It allows you to draw over your image with drawfreehand and then plots the image values underneath where you drew:

Steven Lord
Steven Lord el 9 de Mzo. de 2023
Others have spoken about the mathematics behind taking coordinate data of your math and generating the Z data. If you're having difficulty in the first part of the problem, generating the coordinate data, some options include using the ginput function to let you click points on the plot (for which I'd probably use a 2-D view of your surface, view(2), so you're clicking from "directly overhead") or using the improfile function from Image Processing Toolbox (if you're operating on image data or can treat your data as image data.)

Contoso Donoso
Contoso Donoso el 10 de Mzo. de 2023
Thanks a lot to all the people who replied. I really appreciate your time and effort. It has helped me a lot to learn and save time <3

Categorías

Más información sobre Geographic Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by