Matlab:绘制具有hold on和hold off的子图,在循环中不调用xlabel、ylabel、xlim等

时间:2022-03-12 23:17:14

Matlab question: this might be really simple but I can't figure it out...I'm pretty new. I have a plot window, broken up into two subplots, lets call them A and B, which have different labels and limits. I (hold on), make several plots to B, then I (hold off), then start iterating. In the loop, I want to update both A and B with NEW plots, but I want the axis labels, and xlim and ylim to stay the same, WITHOUT having to call xlabel, xlim, etc every iteration.

Matlab问题:这可能很简单,但我搞不懂……我很新的。我有一个绘图窗口,分成两个小块,我们叫它们a和B,它们有不同的标签和限制。I (hold),画几个图到B,然后I (hold off),然后开始迭代。在循环中,我希望用新的图表更新A和B,但是我希望轴标签,以及xlim和ylim保持不变,而不需要每次迭代都调用xlabel、xlim等等。

Now, (hold off) destroys all axis properties. How do I save the axis properties so I don't have to keep calling xlabel, etc in the loop? I've tried newplot, setting the Nextplot property, etc to no avail. I'd like a simple solution please...not something like re-writing the plot command. Thanks!

现在,(暂停)销毁所有轴属性。如何保存axis属性,从而不必在循环中一直调用xlabel等等?我尝试过newplot,设置Nextplot属性,等等都没用。我想要一个简单的解决办法。不像重写plot命令。谢谢!

hfig=figure();
hax = axes('Parent',hfig);
plot(hax,x,y);
hold on
plot(hax,x1,y1);
%this hold off resets the axes
hold off
while (1)
  subplot('Position',[.07 .05 .92 .44]);
  %I want to do this without having to call xlabel, ylabel, etc
  %over and over
  plot(newx, newy);
  xlabel()
  ylabel()
  hold on
  plot(newx1, newx2)
  hold off
...
end

1 个解决方案

#1


2  

One solution here is to initialize your plot and axes properties before your loop, then within your loop set the 'NextPlot' property of the axes to 'replacechildren' so that only the plot objects (and not the axes settings) will be changed on the next call to PLOT:

这里的一个解决方案是在循环之前初始化plot和axes属性,然后在循环中将坐标轴的“NextPlot”属性设置为“replacechildren”,以便在下一次调用plot时只修改plot对象(而不是坐标轴设置):

hFigure = figure();
hAxes = axes('Parent',hFigure);
plot(hAxes,x,y);
hold on;
plot(hAxes,x1,y1);
xlabel(...);  %# Set the x label
ylabel(...);  %# Set the y label
xlim([...]);  %# Set the x limits
ylim([...]);  %# Set the y limits
while (1)
  set(hAxes,'NextPlot','replacechildren');
  plot(hAxes,newx,newy);
  hold on;
  plot(hAxes,newx1,newx2);
  ...
end

This should maintain the settings for hAxes when new data is plotted in the loop.

当在循环中绘制新数据时,这应该维护hAxes的设置。

#1


2  

One solution here is to initialize your plot and axes properties before your loop, then within your loop set the 'NextPlot' property of the axes to 'replacechildren' so that only the plot objects (and not the axes settings) will be changed on the next call to PLOT:

这里的一个解决方案是在循环之前初始化plot和axes属性,然后在循环中将坐标轴的“NextPlot”属性设置为“replacechildren”,以便在下一次调用plot时只修改plot对象(而不是坐标轴设置):

hFigure = figure();
hAxes = axes('Parent',hFigure);
plot(hAxes,x,y);
hold on;
plot(hAxes,x1,y1);
xlabel(...);  %# Set the x label
ylabel(...);  %# Set the y label
xlim([...]);  %# Set the x limits
ylim([...]);  %# Set the y limits
while (1)
  set(hAxes,'NextPlot','replacechildren');
  plot(hAxes,newx,newy);
  hold on;
  plot(hAxes,newx1,newx2);
  ...
end

This should maintain the settings for hAxes when new data is plotted in the loop.

当在循环中绘制新数据时,这应该维护hAxes的设置。