MATLAB - 在一个图中绘制多个曲面拟合

时间:2015-03-01 09:38:22

标签: matlab plot colors 3d surface

我有3组3D坐标,我已经为每套装配了飞机。现在,我想在一个图中绘制所有数据点和3个平面。

到目前为止,我有以下功能:

function [fitresult, gof] = create_fit(xx, yy, zz, grp)

[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );

ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];

hold on;
% figure( 'Name', 'fit1' );
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
if grp == 1
    colormap(hot);
elseif grp == 2
    colormap(cool);
else
    colormap(grey);
end

legend( h, 'fit1', 'zz vs. xx, yy', 'Location', 'NorthEast' );

xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
grid on

然而,这有两个问题:

  1. 单独绘制平面时,将根据数据点进行缩放,这些数据点也随之绘制。当平面一起绘制时,它们会严重缩放,数据点会收敛到一个小斑点(与平面相比非常小) the points have converged to a small area, but maintain their shape when zoomed in 我尝试使用axis([-0.04 0.04 -0.04 0.04 -0.04 0.04 -1 1]);解决问题,但它是硬编码的,但看起来还是有些偏差。 after axis command

  2. colormap命令似乎仅在第一次调用时才起作用。因此,所有的飞机都变成了蓝色。如何以不同方式为每个平面和适合该平面的点着色?

  3. 这是绘制多个平面的最佳方式吗?

1 个答案:

答案 0 :(得分:1)

修改

这是我最初答案的编辑版本。 plot的输出是一个双元素图形对象,因此您必须单独调用h(1)h(2)来设置平面和数据点的属性。

以下是该函数的代码:

function [fitresult, gof, h] = create_fit(xx, yy, zz, color)

[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );

ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];

hold on;
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );

h = plot( fitresult, [xData, yData], zData);

set(h(1), 'FaceColor', color);
set(h(2), 'MarkerFaceColor', color, 'MarkerEdgeColor', 'k');

这是一个调用函数的示例脚本:

% Define sample data
N = 20;
x = rand(1,N);
y = rand(1,N);
z = rand(1,N);

% Call the function, specify color
[f1, gof1, h1] = create_fit(x, y, z, 'r');
[f2, gof2, h2] = create_fit(x, y.^10, z, 'g');
[f3, gof3, h3] = create_fit(x.^10, y, z, 'b');

% Figure adjustments
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
view(3)
grid on

xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
zlim([min(z) max(z)]);

结果:

enter image description here