필터 지우기
필터 지우기

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

조회 수: 58 (최근 30일)
Contoso Donoso
Contoso Donoso 2023년 3월 9일
답변: Contoso Donoso 2023년 3월 10일
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.

채택된 답변

Cameron
Cameron 2023년 3월 9일
편집: Cameron 2023년 3월 9일
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
Cameron
Cameron 2023년 3월 9일
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 2023년 3월 10일
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

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Image Analyst
Image Analyst 2023년 3월 9일
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 2023년 3월 9일
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 2023년 3월 10일
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

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by