MATLAB - 在一个图中绘制多个曲面拟合 [英] MATLAB - Plot multiple surface fits in one figure

查看:85
本文介绍了MATLAB - 在一个图中绘制多个曲面拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 组 3D 坐标,并且每组都安装了平面.现在,我想在一个图中绘制所有数据点和 3 个平面.

I have 3 sets of 3D co-ordinates and I have fitted planes to each set. Now, I want to plot all the data points and the 3 planes in one figure.

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

So far, I have the following function:

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. 平面在单独绘制时根据数据点进行缩放,这些数据点也与它们一起绘制.当平面绘制在一起时,它们的缩放比例很差,数据点会收敛到一个小斑点(与平面相比,比例非常小)我尝试用 axis([-0.04 0.04 -0.04 0.04 -0.04 0.04 -1 1]); 解决这个问题,但它是硬编码的,看起来仍然有点不对劲.

  1. The planes, when plotted individually are scaled according to the data points which are also plotted with them. When the planes are plotted together, they scale badly and the data points are converge to a small blob (very small scale compared to the planes) I tried fixing the problem with axis([-0.04 0.04 -0.04 0.04 -0.04 0.04 -1 1]);, but it is hard coded and still looks a little off.

colormap 命令似乎只有在第一次调用时才起作用.因此,所有的飞机都是蓝色的.我如何为每个平面和适合该平面的点着色不同?

The colormap command seems to work only when called for the first time. Hence, all the planes turn out to be blue. How can I color each plane and the points fitted for that plane differently?

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

Is this the best way to plot multiple planes?

推荐答案

编辑

这是我最初回答的编辑版本.plot 的输出是一个二元图形对象,所以必须分别调用h(1)h(2) 来设置平面和数据点的属性.

Here is an edited version of my initial answer. The output of plot is a two-element graphical object, so you have to call separately h(1) and h(2) to set the properties of the plane and of the data points.

这是函数的代码:

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');

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

and here is a sample script that calls the function:

% 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)]);

结果:

这篇关于MATLAB - 在一个图中绘制多个曲面拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆