Interpolated 2D sections out of 3D plot

I have a number of data for 3 variables - data points in 3D - which can be plotted using plot3(). I want to make 2D sections from this plot, keeping one of the 3 variables, lets call it z, constant at different values (set up an array of constant z values and make 2D sections in x-y plane for each value). I want to use interpolation to get the most accurate 2D section. Any help on how I can do this?

2 Comments

Do you have an example? And some data if possible please.
I cannot post the real data but lets say we have a data array of 3 columns and 100 rows (100 data points). I plot in 3D using
plot3(data(:,1),data(:,2),data(:,3))
Then I wanna make, lets say, ten different 2D plots in x-y-plane for z=[81:1:90].
How do I proceed for the interpolation of the data points?

Sign in to comment.

 Accepted Answer

darova
darova on 27 Sep 2019
Use griddata() (use griddata(x,y,z,xq,yq,'method','cubic') if interpolation is needed) to build a surface
Use contour() to create a crossection at specific height

5 Comments

Thank you for the answer! And yes, I have tried both.
But for the interpolation, how am I suppose to have two arrays of query points (xq, yq)? I only know what z-level I want to interpolate at, i.e. I only have one array of query points (the const z values).
For contour(), I dont understand how I am suppose to give the correct input to this function with the data points that I have. Each column of the data matrix is one dimension and there are 3 columns (3 dimensions). Each row in the data matrix corresponds to one data point.
I suppose I need to use meshgrid() somehow, but I dont understand how...
You have some data (x,y,z)
You'll need to know boundaries of your data (where x and y begins and ends)
Once you know boundaries you can build a mesh
xq = linspace( min(x), max(x), 20 ); % boundary of x
yq = linspace( min(y), max(y), 20 ); % boundary of y
[X,Y] = meshgrid(xq,yq);
Don't worry about areas for those you don't have data (griddata() will place there a NaN value)
Z = griddata(x,y,z,X,Y,'method','cubid');
Once you have X,Y,Z as 2D matrix you can build a surface
surf(X,Y,Z)
and contour also
height = [1 2 3]; % height you want crossection at
hold on
contour(X,Y,Z,height)
hold off
see also contourf(), contour3()
Do you want to extract those contour data?
This helped me a lot, thank you!
Quick question, using contour(X,Y,Z) it chooses the levels to plot in the contour itself, but when I specify a height array of levels by typing contour(X,Y,Z,height), those are the levels plotted?
Yes it would be nice to extract them as well!
those are the levels plotted?
The answer is YES!
To extract contours: LINK
See HELP how data is stored ic ContourMatrix
contour_matrix_diagram.png
Please accept the answer if it helped

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!