Matlab 轴缩放

发布于 2024-09-30 06:29:18 字数 143 浏览 1 评论 0原文

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

我知道 hold on 可以解决问题,但我不想看到旧数据。

how exactly do you get fixed scaling of axes in Matlab plot when plotting inside a loop? My aim is to see how data is evolving inside the loop. I tried using axis manual and axis(...) with no luck. Any suggestions?

I know hold on does the trick, but I don't want to see the old data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2024-10-07 06:29:18

如果您希望看到新的绘制数据替换旧的绘制数据,但保持相同的轴限制,您可以使用 SET 循环中的命令。这是一个简单的示例:

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

If you want to see your new plotted data replace the old plotted data, but maintain the same axes limits, you can update the x and y values of the plotted data using the SET command within your loop. Here's a simple example:

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

When you run the above, you will see an asterisk jump around in the axes for a couple of seconds, but the axes limits will stay fixed. You don't have to use the HOLD command because you are just updating an existing plot object, not adding a new one. Even if the new data extends beyond the axes limits, the limits will not change.

囚我心虐我身 2024-10-07 06:29:18

您必须设置轴限制;理想情况下,您可以在开始循环之前执行此操作。

这行不通

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

You have to set the axes limits; ideally you do that before starting the loop.

This won't work

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

This will work

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文