8

在循环内绘图时,您如何在 Matlab 图中获得固定的轴缩放比例?我的目标是了解数据在循环中是如何演变的。我尝试使用axis manualaxis(...)没有运气。有什么建议么?

我知道hold on有诀窍,但我不想看到旧数据。

4

2 回答 2

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命令,因为您只是更新现有绘图对象,而不是添加新对象。即使新数据超出坐标区限制,限制也不会改变。

于 2010-11-03T17:28:58.690 回答
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
于 2010-11-03T11:40:35.973 回答