Matlab:用3个向量绘制等高线图

时间:2012-09-27 05:32:55

标签: matlab plot contour

我有3个数据向量,X(位置),Y(位置),两者都没有规则间隔,Z(每个位置感兴趣的值)。我试过了contourf,它不起作用,因为它需要一个Z输入矩阵。

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:6)

对于contour绘图,您实际上需要z值矩阵或网格上评估的z值集合(矢量)。您无法使用网格上(X,Y)点的孤立Z值定义轮廓(即您声称拥有的)。

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

如果没有,那么您可以创建a surface from nonuniform data 为@nate正确指出,然后在该表面上绘制轮廓。

考虑以下(随机)示例:

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

圆圈对应于每个点具有值(x,y,z)的原始矢量点,插值曲面的轮廓上的轮廓。   enter image description here   enter image description here