如何在循环线性图上获取动态图例?

时间:2016-10-01 13:27:00

标签: matlab dynamic matlab-figure legend linear

Diff条件:如何在每次迭代结束时清除动态图例;如何在每次迭代结束时删除拟合的线性线。

我正在尝试扩展线程Dynamic Legend (Updates in every recursion)的这个答案,以在一个图表上迭代图例。 建议在一个线性图上覆盖动态图例

close all; clear all; 

% Test data
aSize=zeros(2,777);
aSize=[[0, 0]' randi(3,2,777)]; % STDEV about 3

x=0:1:180; 

hFig=figure; 

index=1;
while (index<=7); 

    % origo left alone
    aSize(:, index+1) = aSize(:, index+1) + index; % to have linearly increasing trend

    sz=40; 
    legend('-DynamicLegend');
    h = scatter(aSize(1,1:index+1)', aSize(2,1:index+1)', sz, ...
          'MarkerEdgeColor',[0 .5 .5],...
          'MarkerFaceColor',[0 .7 .7],...
          'LineWidth',1.5, ...
          'DisplayName', sprintf('Data'));
    xlabel('width'); ylabel('height');
    hold on; 

    % Optimum
    x=0:1:max( aSize(1, 1:index+1) ); 
    y = x; % assume uniform
    grid on; 
    h1=plot(x,y, 'black', ...
        'DisplayName', sprintf('Optimum'));

    % Fit with R2 linear
    x=aSize(1,1:index+1); 
    b1 = aSize(1,1:index+1)' \ aSize(2,1:index+1)'; 
    yCalc1 = b1 * aSize(1,1:index+1);
    Rsq1 = 1 - sum((y(1:index+1) - yCalc1).^2)/sum((y(1:index+1) - mean(y(1:index+1))).^2)

    % origo needed
    x = [0 x]; 
    yCalc1 = [0 yCalc1]; 
    h2=plot(x(1:index+2)', yCalc1(1:index+2)', '--p', ...
        'DisplayName', sprintf('Fit R2 = %d', Rsq1)); 

    drawnow; 

    index=index+1;

end;

仅在附加图例的情况下输出不成功

enter image description here

Matlab:2016a
操作系统:Debian 8.5 6位
Linux内核:4.6的backports
硬件:华硕Zenbook UX303UA

1 个答案:

答案 0 :(得分:1)

删除此行legend('-DynamicLegend');并在drawnow行之前写下,然后在该行后面写hold off。所以以下是循环结束时的行。

legend('-DynamicLegend');
drawnow; 
hold off
index=index+1;