2010-11-03 149 views
8

在Matlab中繪製循環內圖時,您如何精確確定座標軸的縮放比例?我的目標是瞭解數據在循環內部的演變情況。我嘗試使用axis manualaxis(...),但沒有運氣。有什麼建議麼?Matlab座標軸縮放

我知道hold on有竅門,但我不想看到舊的數據。

+2

您可能還想閱讀有關未公開的功能「LimInclude」:http://undocumentedmatlab.com/blog/plot-liminclude-properties/ – Amro 2010-11-03 23:46:14

回答

6

如果你想看到你的新繪製的數據代替舊繪製的數據,但保持在同一座標的限制,你可以使用循環內的SET命令更新繪圖數據的x和y值。這裏有一個簡單的例子:

hAxes = axes;      %# Create a set of axes 
hData = plot(hAxes,nan,nan,'*'); %# Initialize a plot object (NaN values will 
            %# keep it from being displayed for now) 
axis(hAxes,[0 2 0 4]);   %# Fix your axes limits, with x going from 0 
            %# to 2 and y going from 0 to 4 
for iLoop = 1:200     %# Loop 100 times 
    set(hData,'XData',2*rand,... %# Set the XData and YData of your plot object 
      'YData',4*rand);  %# to random values in the axes range 
    drawnow       %# Force the graphics to update 
end 

當你運行上面,你會看到周圍的星號躍在軸的幾秒鐘,但軸限制將保持固定。您不必使用HOLD命令,因爲您只是更新現有的繪圖對象,而不是添加新的繪圖對象。即使新數據超出軸限制,限制也不會改變。

+4

+1我也有一些建議:1)爲避免閃爍,你應該啓用雙緩衝'set(gcf,'DoubleBuffer','on')'。 2)如果你想增加繪圖的速度並獲得更平滑的動畫,可以將'EraseMode'屬性設置爲'normal'以外的東西(在這種情況下我會使用'xor')。當然,你必須使用低級函數,如線條,補丁,文本等。查看本指南瞭解更多詳情:http://www.mathworks.com/support/tech-notes/1200/1204。 HTML#壩段%2023 – Amro 2010-11-04 00:11:56

1

您必須設置軸限制;理想情況下,你在開始循環之前要這樣做。

這將無法正常工作

x=1:10;y=ones(size(x)); %# create some data 
figure,hold on,ah=gca; %# make figure, set hold state to on 
for i=1:5, 
    %# use plot with axis handle 
    %# so that it always plots into the right figure 
    plot(ah,x+i,y*i); 
end 

這將工作

x=1:10;y=ones(size(x)); %# create some data 
figure,hold on,ah=gca; %# make figure, set hold state to on 
xlim([0,10]),ylim([0,6]) %# set the limits before you start plotting 
for i=1:5, 
    %# use plot with axis handle 
    %# so that it always plots into the right figure 
    plot(ah,x+i,y*i); 
end