MATLAB - Plot multiple surface fits in one figure

三世轮回 提交于 2019-12-23 01:57:21

问题


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

However, there are 2 problems with this:

  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.

  2. 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?

  3. Is this the best way to plot multiple planes?


回答1:


Edit

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.

Here is the code for the function:

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

and the result:



来源:https://stackoverflow.com/questions/28792047/matlab-plot-multiple-surface-fits-in-one-figure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!