just to clarify, the addpoints code works fine in the loop as long as it's not in a subplot.
animatedline fails after the first iteration
1 次查看(过去 30 天)
显示 更早的评论
Hi,
It was previously working but now I am getting an empty plot in the second subplot after the first iteration. Code is as follows:
for i=1:10:length(tu)
ax1= subplot(2,1,1)
pcolor(lontemp(findnearest(150,lontemp):findnearest(160, lontemp)),lattemp, hc')
Y1=get(gca,'ylim');
set(ax1,'dataaspectratio',[.7 cos((Y1(2)+Y1(1))/5/180*pi) 1], 'position',[0.204,0.5,0.55,0.42])
ax2=subplot(2,1,2)
if i==1
curve1=animatedline;
curve2=animatedline;
curve3=animatedline;
curve2.Color='blue';
curve3.Color='red';
set(ax2,'Xlim',[1 365],'Ylim', [min(T1) max(T1)], 'Position',[0.13,0.11,0.775,0.29], 'plotboxaspectratio',[1,0.31,0.31])
grid on
addpoints(curve1,m(i),n(i));
hold on
addpoints(curve2,m(i),T1(i));
addpoints(curve3,m(i),T2(i));
drawnow;
pause(0.1)
else
grid on
addpoints(curve1,m(i),n(i));
addpoints(curve2,m(i),T1(i));
addpoints(curve3,m(i),T2(i));
drawnow;
pause(.1);
end
end
采纳的回答
Walter Roberson
2020-8-6
Create the subplot and modify their position before the loop. Make sure to pass the axes to the hold() and grid() calls.
At present you keep calling subplot and then modify the position. However each time you call subplot it figures out where the plot belongs based on the n m index parameters, and if it finds any axes that over overlap those coordinates but not exactly the same coordinates then it deletes the old one. So subplot() all your axes into existence first before you set the position of any of them, and record the axes handles and do not subplot in a loop unless you do not mind old axes potentially being deleted.
4 个评论
dpb
2020-8-7
pcolor as above would be totally immaterial to subplot(2,1,2)
Does not the above generate the expected lines?
Walter Roberson
2020-8-7
hAx1 = subplot(2,1,1);
hAx2 = subplot(2,1,2);
pcolor(hAx1, lontemp(findnearest(150,lontemp):findnearest(160, lontemp)),lattemp, squeeze(hc(:,:,i))');
Y1 = ylims;
%postpone setting the Position of the subplots until all subplots are created
set(hAx1, 'dataaspectratio', [.7 cos((Y1(2)+Y1(1))/5/180*pi) 1], 'position', [0.204,0.5,0.55,0.42])
set(hAx2, 'Xlim', [1 365], 'Ylim', [min(T1) max(T1)], 'Position', [0.13,0.11,0.775,0.29], 'plotboxaspectratio', [1,0.31,0.31])
grid(hAx2, 'on')
hold(hAx2, 'on')
curve1 = animatedline(hAx2);
curve2 = animatedline(hAx2,'color','blue');
curve3 = animatedline(hAx2,'color','red');
for i = 1:10:length(tu)
addpoints(curve1,m(i),n(i));
addpoints(curve2,m(i),T1(i));
addpoints(curve3,m(i),T2(i));
drawnow;
pause(0.1)
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!