2015-12-02 57 views
1

到目前爲止,我有一個3d實時更新的凱塞爾運行的蒙託卡羅模擬。我希望繪圖在更新時可以旋轉,但啓用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 

每當我更新繪圖中的數據時,都會執行此操作。我可以添加什麼以確保在更新期間保持軸旋轉?

回答

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,YDataZData。這是可以實現如下:

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避免復位。

+0

哦,這比我剛剛提出的解決方案更好。我的想法是跟蹤圖的'CurrentAxes'的'CameraPosition'和'CameraViewAngle',並將這些值重新設置爲之前的值,但這更直接地解決了問題,謝謝! –

+0

@PatrickRoberts聽起來像這樣也可以。但既然你可以完全避免重置,那當然更好。修改'XData'&co。是在更新繪圖或循環繪製時嘗試嘗試的默認技巧之一:) – mikkola

0

我有同樣的問題,在Matlab 2015b中,你可以用這種簡單的方法解決它。

[az,el] = view; 
scatter3(x,y,z); 
view(az,el); 
drawnow 
相關問題