Matlab:'hold on'与'set()'不兼容吗? [英] Matlab: 'hold on' doesn't work with 'set()'?
问题描述
我正在使用plot()
和hold(Ax, 'on')
函数在同一轴上绘制多个图.但是,我注意到,如果我使用set(Ax, 'XData', ..., 'YData', ...)
而不是plot()
进行绘图,则hold(Ax, 'on')
功能将不起作用.换句话说,如果使用set()
函数,则无法在同一轴上一起绘制所有曲线.知道为什么吗,或者是否有一种方法可以使用set()
而又使用hold on
功能?谢谢!
I am plotting multiple plots on the same axes by using plot()
and hold(Ax, 'on')
function. However, I have noticed that if I use set(Ax, 'XData', ..., 'YData', ...)
for plotting instead of plot()
then the hold(Ax, 'on')
functionality does not work. In other words I am not able to plot all the curves together on same axes if I use set()
function. Any idea why is that, or if there is a way by which I can use set()
and yet use the hold on
functionality? Thanks!
推荐答案
基于此答案,您可以尝试以下示例:
Based on this answer, you can try something like the following example:
x1 = linspace(1,10,21);
y1 = rand(1,21);
x2 = x1;
y2 = rand(1,21);
x3 = x2;
y3 = rand(1,21);
h(1) = plot(x1,y1,'b');
h(2) = copyobj(h(1),gca);
h(3) = copyobj(h(1),gca);
set(h(2),'XData',x2,'YData',y2,'Color','r')
set(h(3),'XData',x3,'YData',y3,'Color','g')
set
命令更新所引用对象的属性通过手柄.如果您不想一遍又一遍地修改一条曲线,而是想添加一条曲线,则必须使用
The set
command updates the properties of the object referenced by the handle. If you don't want to modify your one curve over and over, but add curves, you have to copy the initial line object, using copyobj
. Once you do that, you can generate an infinite amount of additional lines for which you can then set the properties (including new 'XData'
, 'YData'
as you please.
However, you will need an initial line object to copy and modify.
Also, depending on how you set up your plot, consider replacing gca
by your axis handle so that the reference is correct.
这篇关于Matlab:'hold on'与'set()'不兼容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!