Contour plot using three vectors

99 views (last 30 days)
SS
SS on 12 Nov 2019
Commented: Star Strider on 12 Nov 2019
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.
  2 Comments
darova
darova on 12 Nov 2019
Did you tri griddata?
SS
SS on 12 Nov 2019
No, I got stuck with that.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 12 Nov 2019
I thought we already solved that in Contour plot of concentration of chemical species ?
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
SS
SS on 12 Nov 2019
Edited: SS on 12 Nov 2019
Hi, thanks for your answer. I am trying to understand how to make contour plots in MATLAB. I have three questions:
(1). Why have you used [X Y] = ndgrid(xv,yv) instead of [X Y]=ndgrid(x,y). I am unable to understand the difference.
(2). I was reading up on some questions asked in the community. I am wondering why, you have used ndgrid instead of meshgrid in the code. Apart from the order difference, I am not sure what is the effect of these grid generating schemes on the result or the type of input data. I have tried both methods and see no difference in the contour plot. Can you throw some light on it?
(3). How can, I have a smooth filled contour plot? I mean, I don not want a black line seperating the regions of diffrent colors.
I really appreciate if, you can direct me towards some source where I can learn about the detailed steps in creating contour plots.
Star Strider
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.

Sign in to comment.

More Answers (1)

KSSV
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
SS
SS on 12 Nov 2019
Edited: SS on 12 Nov 2019
Thanks, I got the follwoing error:
Error in tricontf (line 82)
[Xp,Yp,M,Zp,nv,CS,xx,yy,zz]=tricont(args{1:nin});
KSSV
KSSV on 12 Nov 2019
Read the help in there with the function and try to input the inputs in the correct way.

Sign in to comment.

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!