当我添加新输入时,如何在Matlab图中逐渐淡出先前的输入

问题描述 投票:16回答:3

假设我有这个非常简单的循环

for i=1:10
    [xO, yO, xA, yA, xB, yB, xC, yC] = DoSomething(i);
    line([xO,xA,xB,xC],[yO,yA,yB,yC]);
    pause(0.1);
end

我正在绘制的坐标对应于多体系统的关节,我正在模拟它们随时间的位置(请参见此处的样本示例):

由于一些链接以周期性方式移动,因此在视觉上跟踪移动会变得混乱。出于这个原因,现在出现了一个问题:如何绘制线条,当绘制新线条时,前面的线条会逐渐消失?换句话说,我有一个从最近绘制的数据(最不透明)到最旧数据的渐变(越来越透明,直到它完全淡出)。

这种方式当一条新线在与旧数据相同的位置绘制时,我会注意到它是一个新线。

matlab plot line transparency matlab-figure
3个回答
6
投票

我正在添加第二个答案,以明确区分两种完全不同的方法。我的1st answer使用未记录的(以及2018b,折旧)透明度选项作为行。

这个答案为绘图提供了一种不同的方法,没有兼容性问题(这两个'特征'可以独立实现):

  • 创建一个固定的n线并更新它们的位置,而不是创建越来越多的线。
  • 重新着色线条,淡化为白色,而不是改变透明度。

这是代码,请参阅注释以获取详细信息:

% "Buffer" size, number of historic lines to keep, and governs the 
% corresponding fade increments.
nFade = 100;

% Set up some demo values for plotting around a circle
dt = 0.05; a = 0:dt:2*pi+(dt*nFade); n = numel(a); b = a.*4;
[x1,y1] = pol2cart( a, ones(1,n) ); [x2,y2] = pol2cart( b, 0.4*ones(1,n) ); 
x = [zeros(1,n); x1; x1+x2]; y = [zeros(1,n); y1; y1+y2]; 

% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1.5,1.5]); ylim([-1.5,1.5]);

% Draw all of the lines, initially not showing because NaN vs NaN
lines = arrayfun( @(x)line(NaN,NaN), 1:nFade, 'uni', 0 );
% Set up shorthand for recolouring all the lines
recolour = @(lines) arrayfun( @(x) set( lines{x},'Color',ones(1,3)*(x/nFade) ), 1:nFade );

for ii = 1:n
    % Shift the lines around so newest is at the start
    lines = [ lines(end), lines(1:end-1) ]; 
    % Overwrite x/y data for oldest line to be newest line
    set( lines{1}, 'XData', x(:,ii), 'YData', y(:,ii) );
    % Update all colours
    recolour( lines );           
    % Pause for animation           
    pause(0.01);
end

结果:

demo


18
投票

您可以通过修改过去行的第4个Color属性来完成此操作。

这是一个演示结果gif,每帧我淡出10%的透明度,所以只有最近的10行是可见的。

fade plot demo

这是代码,请参阅我的评论以获取详细信息:

% Set up some demo values for plotting around a circle
a = 0:0.1:2*pi; n = numel(a);
[x,y] = pol2cart( a, ones(1,n) );

% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1,1]); ylim([-1,1]);
% Array of graphics objects to store the lines. Could use a cell array.
lines = gobjects( 1, n );
% "Buffer" size, number of historic lines to keep, and governs the 
% corresponding fade increments.
nFade = 10;

% Main plotting loop
for ii = 1:n
    % Plot the line
    lines(ii) = line( [0,x(ii)], [0,y(ii)] );
    % Loop over past lines.
    % Note that we only need to go back as far as ii-nFade, earlier lines
    % will already by transparent with this method!
    for ip = max(1,ii-nFade):ii
        % Set the 4th Color attribute value (the alpha) as a percentage
        % from the current index. Could do this various ways.
        lines(ip).Color(4) = max( 0, 1 - (ii-ip)/nFade );
    end
    % Delay for animation
    pause(0.1);
end

如果你有很多行,你可能想要做一些情节/内存管理。您可以通过添加类似的内容来删除透明线条

if lines(ii).Color(4) < 0.01
    delete(lines(ii));
end

循环内。这样你的数字就不会有大量的透明残余。


笔记:

  • 我生成了实际的gif using imwrite以防万一。
  • 显然,R2018b已经折旧了第4个颜色值“功能”(不确定它是否已正式记录)。

有足够的upvotes来激励制作一个稍微有趣的演示......

fader


16
投票

Matlab 2018a或更高版本(或更早,至少晚于2012a)的解决方案

由于Matlab 2018a不再支持第四个颜色参数作为alpha值(并且显然从未像Cris Luengo所指出的那样),这里有一个解决方案,可以在Matlab 2018a中使用来自文件交换的patchline函数(信用到Brett Shoelson) )。

% init the figure
figure(); axes();
hold on; xlim([-1 0.5]); ylim([0 1]);

% set fraction of alpha value to take
alpha_fraction = 0.7;
n_iterations = 200;

% looping variable to prevent deleting and calling already deleted lines
% i.e. to keep track of which lines are already deleted
delete_from = 1;

for i=1:n_iterations
    % your x, y data
    [x, y] = doSomething(i);

    % create line with transparency using patchline
    p(i) = patchline(x,y, 'linewidth', 1, 'edgecolor', 'k');

    % set alpha of line to fraction of previous alpha value
    % only do when first line is already plotted
    if i > 1
        % loop over all the previous created lines up till this iteration
        % when it still exists (delete from that index)
        for j = delete_from:i-1       
            % Update the alpha to be a fraction of the previous alpha value
            p(j).EdgeAlpha = p(j).EdgeAlpha*alpha_fraction;

            % delete barely visible lines
            if p(j).EdgeAlpha < 0.01 && delete_from > j
                delete(p(j));
                % exclude deleted line from loop, so edgealpha is not
                % called again
                delete_from = j;
            end
        end
    end
    % pause and behold your mechanism 
    pause(0.1);
end

我包括删除几乎看不见的行,正如@Wolfie所建议的那样(我自己的,可能不那么优雅的实现)

这里有一个快速释放机制的演示:

Quick-release-mechanism

© www.soinside.com 2019 - 2024. All rights reserved.