【问题标题】:Updating plot while maintaining axes rotations?在保持轴旋转的同时更新绘图?
【发布时间】:2015-12-02 19:42:07
【问题描述】:

到目前为止,我有一个 3d 图,它在 Kessel 运行的 Monto-Carlo 模拟过程中实时更新。我希望绘图在更新时可旋转,但启用rotate3d 标志后,当我在更新绘图数据后调用drawnow 时,它将轴旋转重置为默认方向,取消用户将其更改为的任何内容。我的相关代码如下:

s = scatter3(pg(:,1), pg(:,2), pg(:,3), 'O', 'filled');
set(s, 'MarkerEdgeColor', 'none', 'MarkerFaceColor', 'k');

hold on

p_x = curr_path(:,1);
p_y = curr_path(:,2);
p_z = curr_path(:,3);

p = plot3(p_x, p_y, p_z);
set(p, 'LineWidth', 2, 'Color', 'k');

p_x = longest.path(:,1);
p_y = longest.path(:,2);
p_z = longest.path(:,3);

p = plot3(p_x, p_y, p_z);
set(p, 'LineWidth', 2, 'Color', 'r');

p_x = shortest.path(:,1);
p_y = shortest.path(:,2);
p_z = shortest.path(:,3);

p = plot3(p_x, p_y, p_z);
set(p, 'LineWidth', 2, 'Color', 'b');

sample_str = sprintf('%d samples', sample);
short_str = sprintf('shortest: %g parsecs', shortest.dist);
long_str = sprintf('longest: %g parsecs', longest.dist);

title(sprintf('%s, %s, %s', sample_str, short_str, long_str));

xlim([-10 10]);
ylim([-10 10]);
zlim([-10 10]);

hold off

drawnow

每次我更新绘图上绘制的数据时都会执行此操作。我可以添加什么来确保在更新期间保持轴旋转?

【问题讨论】:

    标签: matlab plot rotation axes


    【解决方案1】:

    我希望我能正确理解你的问题。好了,就这样吧!

    我认为问题与使用plot3 有关,这显然会将图形的视图设置重置为默认值。

    我用以下程序验证了这一点:

    figure; 
    x = randn(10,3);
    p = plot3(x(:,1), x(:,2), x(:,3));
    drawnow
    
    pause; %// do rotations in the figure GUI, press a button when done
    
    x = randn(10,3);
    p = plot3(x(:,1), x(:,2), x(:,3)); %// rotations reset
    drawnow;
    pause
    

    当暂停处于活动状态时,您可以旋转图形,但是当暂停被释放并第二次调用plot3 时,旋转设置被重置。

    避免重置的解决方案是直接更新已绘制图形对象中的XData、YData和ZData。这可以通过以下方式实现:

    figure; 
    x = randn(10,3);
    p = plot3(x(:,1), x(:,2), x(:,3));
    drawnow
    
    pause; %// do rotations in the figure GUI, press a button when done
    
    x = randn(10,3);
    set(p, 'XData', x(:,1), 'YData', x(:,2), 'ZData', x(:,3)); %// no reset!
    drawnow;
    pause
    

    所以无论你有什么代码,使用图形对象的句柄直接更新line properties以避免重置。

    【讨论】:

    • 哦,这比我刚刚提出的解决方案还要好。我的想法是跟踪图形的CurrentAxes 的CameraPosition 和CameraViewAngle,并将这些值设置回原来的值,但这更直接地解决了问题,谢谢!
    • @PatrickRoberts 听起来也可以。但既然你可以完全避免重置,那当然更好。修改XData & co.是我在循环更新绘图或绘图时尝试的默认技巧之一 :)
    【解决方案2】:

    我遇到了同样的问题,在 Matlab 2015b 中你可以用这种简单的方式解决它。

    [az,el] = view;
    scatter3(x,y,z);
    view(az,el);
    drawnow
    

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 2017-12-05
      • 2011-01-26
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      相关资源
      最近更新 更多