Matlab: Make a contour plot with 3 vectors

I have 3 data vectors, X (position), Y (position), both of which are not on a regular basis, and Z (value of interest in each place). I tried a path that doesn't work, because it needs a matrix to enter Z.

+4
source share
2 answers

You can also use griddata .

 %Generate random data x = rand(30,1); y = rand(30,1); z = rand(30,1); %Create regular grid across data space [X,Y] = meshgrid(linspace(min(x),max(x),n), linspace(min(y),max(y),n)) %create contour plot contour(X,Y,griddata(x,y,z,X,Y)) %mark original data points hold on;scatter(x,y,'o');hold off 
+6
source

For the contour graph, you really need either a matrix of z values ​​or a set (vector) of z values ​​evaluated on a grid. You cannot define contours using isolated Z values ​​at points (X, Y) on the grid (that is, as you say you have).

You need the generation process (or function) to provide values ​​for the grid of points (x, y).

If not, you can create a surface from uneven data , as @nate pointed out correctly, and then draw outlines on that surface.

Consider the following (random) example:

 N = 64; % point set x = -2 + 4*rand(N,1); % random x vector in[-2,2] y = -2 + 4*rand(N,1); % random y vector in[-2,2] % analytic function, or z-vector z = x.*exp(-x.^2-y.^2); % construct the interpolant function F = TriScatteredInterp(x,y,z); t = -2:.25:2; % sample uniformly the surface for matrices (qx, qy, qz) [qx, qy] = meshgrid(t, t); qz = F(qx, qy); contour(qx, qy, qz); hold on; plot(x,y,'bo'); hold off 

The circles correspond to the initial vector points with values (x,y,z) for each point, by the contours on the contours of the interpolation surface. enter image description hereenter image description here

+5
source

Source: https://habr.com/ru/post/1436515/


All Articles