2

所以我在 2D 平面上有一个 N 个点的图(N 可以非常大)。我正在编写一个脚本来显示算法的工作原理。所以我有for循环。在 for 循环的每一步,我都想改变当前点的颜色(实际上可能只用这一点制作一个茎图)。

但是,在步骤结束时,我想删除当前点的颜色,以便为下一个点上色。目前我必须重绘整个情节(包括 2D 点)。我不确定 Matlab 是否在绘图命令中检测到这些东西,但有没有办法在不重绘整个绘图的情况下做到这一点?

例如:

plot(x,y, '*');


for j = 1:N-1
    for i = j:N
        hold on;
        %Do stuff

        plot(x,y, '*');
        hold on;
        stem(x(1), y(1), 'g*');
    end

end
4

2 回答 2

7

一个简单的例子:

%# plot some data
x = 1:100;
y = cumsum(rand(size(x))-0.5);
plot(x,y,'*-')

%# animate by going through each point
hold on
h = stem(x(1),y(1),'g');
hold off
for i=1:numel(x)
    %# update the stem x/y data
    set(h, 'XData',x(i), 'YData',y(i));

    %# slow down a bit, drawnow was too fast!
    pause(.1)
end

截屏

于 2012-06-23T01:55:33.560 回答
2

查看句柄图形对象的文档。

我建议将整组点绘制为一个对象。然后,对于每次迭代,绘制兴趣点。保存一个句柄(如,h = plot(...);)。当您准备好进行下一次迭代时,delete使用该句柄 ( delete(h)) 的对象,并以相同的方式创建下一个。

%# outside the for loop (do this once)
plot(x,y,'*');

for...
    h = stem(x(i),y(i),'g*');
    ...
    %# next iteration... i has been incremented
    delete(h);
    h = stem(x(i),y(i),'g*');
end
于 2012-06-23T00:44:45.557 回答