Contour plot using three vectors
조회 수: 157 (최근 30일)
이전 댓글 표시
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.
채택된 답변
Star Strider
2019년 11월 12일
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
Star Strider
2019년 11월 12일
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.
추가 답변 (1개)
KSSV
2019년 11월 12일
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
KSSV
2019년 11월 12일
Read the help in there with the function and try to input the inputs in the correct way.
참고 항목
카테고리
Help Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!