MATLAB:将3d曲面拟合到条形图

时间:2017-04-23 16:27:31

标签: matlab matlab-figure

我有一个介于0和1之间的数字矩阵(大约400x400)。这是一个3D条形图的图表

enter image description here

我想在条形图中加入3D曲面。有任何想法吗?矩阵的元素(0和1之间的数字)应该给出每个索引处的表面高度。我想表面只是给我们条形图的一般形状,而不是经历每一点。

1 个答案:

答案 0 :(得分:1)

% init
x = randn(1000,1);
y = randn(1000,1);
nbins = [10 20];

% make histogram
h = histogram2(x,y,nbins)

enter image description here

% set limits and steps
min_x = min(x); max_x = max(x); step_x = (max_x - min_x)/nbins(1);
min_y = min(y); max_y = max(y); step_y = (max_y - min_y)/nbins(2);

% make grid
surf_z = h.Values;
surf_x = [min_x + step_x/2 : step_x : max_x - step_x/2];
surf_y = [min_y + step_y/2 : step_y : max_y - step_y/2];
[xx, yy] = meshgrid(surf_x, surf_y)

% plot 3D surface
surf(xx',yy',surf_z)

enter image description here

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% other variant
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% make grid and interpolant
[xx, yy] = ndgrid(surf_x, surf_y)
F = griddedInterpolant(xx,yy,surf_z,'spline');

% make 3D surface with a some step value
step = 0.01;
[Xq,Yq] = ndgrid(min(surf_x):step:max(surf_x), min(surf_y):step:max(surf_y));
Zq = F(Xq,Yq);

% plot 3D surface
mesh(Xq,Yq,Zq);

enter image description here