Matlab updating subplots and hold on

柔情痞子 提交于 2019-12-22 10:53:15

问题


I am having trouble with updating subplots. I've boiled my problem down to the following example:

win = figure(1);
win.sub1 = subplot(2, 2, 1);
win.sub2 = subplot(2, 2, 2);
win.sub3 = subplot(2, 2, 3);
win.sub4 = subplot(2, 2, 4);

x = 1:1:10;

plot(win.sub2, x, x); %graphs the line y = x in the second subplot, good.
hold on;
plot(win.sub2, x, -x) %ought to plot line y = -x over line y = x, but does not.

When executing the second plot, the first plot disappears despite the hold on. The only thing that seems to make this work is if I use axes(win.sub2), but I'm trying to avoid that because it really slows down my program (plotting 4 subgraphs on one figure, each with about 2 overlaid plots, to create a 1000+ frame movie). I appreciate any assistance. Thanks


回答1:


I am a bit puzzled why your example does not work as expected, but changing hold on; to hold(win.sub2, 'on'); seems to produce the desired result.

Note: when executing your example code, matlab gives me a warning, probably because the second line overwrites win as defined in the first line.




回答2:


The problem seems to be that hold on does not affect the right axis. You can solve it using set(...,'Nextplot','add') on the intended axis. To do it sumultaneaously on all axes, it's much easier if win is an array instead of a struct. And by the way, line 1 is useless (win is overwritten).

So, the code would be:

win(1) = subplot(2, 2, 1);
win(2) = subplot(2, 2, 2);
win(3) = subplot(2, 2, 3);
win(4) = subplot(2, 2, 4);

set(win,'Nextplot','add') %// set this for all axes in variable win

x = 1:1:10;

plot(win(2), x, x); %graphs the line y = x in the second subplot, good.
plot(win(2), x, -x) %ought to plot line y = -x over line y = x, but does not.


来源:https://stackoverflow.com/questions/24247636/matlab-updating-subplots-and-hold-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!