Matlab中的网格图问题

时间:2016-01-17 09:17:22

标签: matlab plot mesh

我想绘制一个meshc等高线图,但轮廓没有按我的意图绘制。

x = linspace(P(1),P(2)); %// x axis
y = linspace(P(3),P(4)); %// y axis
[X1 Y1] = meshgrid(x,y); %// all combinations of x, y
%[X1,Y1] = meshgrid(1:.125:3); 
Z1 = mvnpdf([X1(:) Y1(:)],mu,sigma); %// compute Gaussian pdf
Z2 = reshape(Z1,size(X1)); %// put into same size as X, Y
meshc(X1,Y1,Z2); 
%axis([1 3 1 3 -5 10]);
axis([P(1) P(2) P(3) P(4) -5 10])

上面的代码以这种方式绘制:

meshc plot 1

但我希望它是这样的:

enter image description here

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

要获得绘图之间所需的距离,您可以从Z2矩阵中减去一些数字。我无法弄清楚为什么Matlab以这种方式划分图表,但它有效:

P = [1 3 1 3];
mu = [2 1.1];
sigma = [.09 .003; ...
         .003 .002];

x = linspace(P(1),P(2)); %// x axis
y = linspace(P(3),P(4)); %// y axis
[X1 Y1] = meshgrid(x,y); %// all combinations of x, y
Z1 = mvnpdf([X1(:) Y1(:)],mu,sigma); %// compute Gaussian pdf
Z2 = reshape(Z1,size(X1)); %// put into same size as X1, Y1

Z2 = Z2 - 0.01;

meshc(X1,Y1,Z2); 

axis([P(1) P(2) P(3) P(4) -5 10])

Multivariate distribution and its contour plot

也许你需要玩其他数字才能获得理想的结果。在我的例子中,我减去了一个小数字,以避免改变pdf-plot范围。