Contour plot using three vectors
99 views (last 30 days)
Show older comments
Hi. I want to make a contour plot using three vectors. The vectors "x" and "y" has the X and Y coordinates/positions. The vector "z" has numerical values of a property at corresponding (X,Y). All these vectors are of same size (1 X 50,000). I want to make a contour plot using three vectors. Can someone help me with it? I have tried contour and surf in vain and it didn't help.
Accepted Answer
Star Strider
on 12 Nov 2019
Apparently I did not explain this step well enough:
xv = min(Freq(:,1)):max(Freq(:,1)); % ‘x’ Vector For Interpolation
yv = min(Freq(:,2)):max(Freq(:,2)); % ‘y’ Vector For Interpolation
[X,Y] = ndgrid(xv,yv); % Create Interpolation Grids
Z = griddata(Freq(:,1), Freq(:,2), Freq(:,3), X, Y); % Interpolated Grid Of ‘tally’ Values
The griddata function can interpolate vectors to matrices, here interpolating the three columns of the ‘Freq’ matrix (the three vectors you referred to), to the ‘X’ and ‘Y’ matrices that are created from them. Those matrices are created by ndgrid using the ‘xv’ and ‘yv’ vectors, that are intended to span the entire range of the first two columns of the ‘Freq’ matrix. It then creates the ‘Z’ matrix from them, by mapping the corresponding elements of ‘Freq(:,3)’ to the corresponding elements of ‘Z’.
Your ‘x’ and ‘y’ vectors apparently are not already gridded (they do not regularly repeat in particular patterns, although you did not share those data), so using reshape on them will not work correctly. You must instead interpolate them using griddata.
2 Comments
Star Strider
on 12 Nov 2019
My pleasure.
(1) In my code, ‘x’ and ‘y’ are random vectors, so in order to create coherent matrices, it is necessary to use vectors that have specific structures in order to do the interpolation. I created ‘xv’ and ‘yv’ for that specific purpose. This is how I understand griddata works.
(2) The two functions produce similar matrices, and both are used interchangably in the griddata documentation. If I remember correctly, the output matrices are transposes of those produced by the other function. Use either function to create the matrices for griddata. Some MATLAB functions (I do not remember which ones) prefer meshgrid and will throw an error with ndgrid matrices, however griddata is not one of them.
With respect to contour plots, I would simply direct you to the available documentation on contour and related functions. Feel free to experiment with the variious functions to gain experience with them. Once you read the documentation and are familiar with the function calling syntax, you will likely gain more knowledge from experimenting than anything else.
More Answers (1)
KSSV
on 12 Nov 2019
It depends on how is your data. Is your data a structured data or a unstructured data? YOu have the following options. Let (x,y,z) be your three vectors.
%%structured
xi = unique(x) ; yi = unique(y) ;
[X,Y] = meshgrid(xi,yi) ;
Z = reshape(z,size(X)) ;
figure
surf(X,Y,Z)
%%unstructured
dt = delaunayTriangulation(x,y) ;
tri = dt.ConnectivityList ;
figure
trisurf(tri,x,y,z)
6 Comments
KSSV
on 12 Nov 2019
Read the help in there with the function and try to input the inputs in the correct way.
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!