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

查看:792
本文介绍了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天全站免登陆