Matlab:用三个向量做一个等高线图。

时间:2021-03-23 16:11:45

I have 3 vectors of data, X (position), Y (position) both of which are not regularly spaced, and Z(value of interest at each location). I tried contourf, which doesn't work because it needs a matrix for Z input.

我有3个数据向量,X(位置),Y(位置),它们都不是有规律的间隔,Z(每个位置的值)。我尝试了contourf,它不工作,因为它需要一个矩阵Z输入。

2 个解决方案

#1


6  

You can also use griddata.

您还可以使用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

#2


5  

For a contour plot you actually 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 (X,Y) points on the grid (i.e. what you claim you have).

对于等高线图,你实际上需要一个z值矩阵,或者一个在网格上计算的z值的集合(向量)。您不能在网格(即您声称拥有的)点上使用孤立的Z值定义轮廓。

You need to have the generating process (or function) provide values for a grid of (x,y) points.

您需要有生成过程(或函数)提供网格(x,y)点的值。

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

如果没有,那么您可以创建一个从非统一数据创建的表面,正如@nate正确指出的那样,然后在该表面上绘制轮廓。

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 original vector points with values (x,y,z) per point, the contours on the contours of the interpolant surface. Matlab:用三个向量做一个等高线图。Matlab:用三个向量做一个等高线图。

这些圆对应于每个点的初始矢量点(x,y,z),在插补表面的轮廓上的等值线。

#1


6  

You can also use griddata.

您还可以使用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

#2


5  

For a contour plot you actually 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 (X,Y) points on the grid (i.e. what you claim you have).

对于等高线图,你实际上需要一个z值矩阵,或者一个在网格上计算的z值的集合(向量)。您不能在网格(即您声称拥有的)点上使用孤立的Z值定义轮廓。

You need to have the generating process (or function) provide values for a grid of (x,y) points.

您需要有生成过程(或函数)提供网格(x,y)点的值。

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

如果没有,那么您可以创建一个从非统一数据创建的表面,正如@nate正确指出的那样,然后在该表面上绘制轮廓。

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 original vector points with values (x,y,z) per point, the contours on the contours of the interpolant surface. Matlab:用三个向量做一个等高线图。Matlab:用三个向量做一个等高线图。

这些圆对应于每个点的初始矢量点(x,y,z),在插补表面的轮廓上的等值线。