Cross section of a plot (surface, Imagesc) using an arbitrary line
50 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Cameron
on 9 Mar 2023
Edited: Cameron
on 9 Mar 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 Comments
Cameron
on 9 Mar 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)
More Answers (3)
Image Analyst
on 9 Mar 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:
0 Comments
Steven Lord
on 9 Mar 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.)
0 Comments
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!