How to create an animated plot?

조회 수: 8 (최근 30일)
Atiqah Zakirah
Atiqah Zakirah 2017년 6월 19일
댓글: KSSV 2017년 6월 19일
I'm trying to combine 24 plots (each plot representing the density of people in a certain area) into one figure (making some what of an animated plot). But instead of my 24 plots being displayed one after the other in the same figure with a short pause in between each one, I end up with 24 separate figures. How do I combine them and make an animated plot? This is the code I have at the moment. Trying to combine them so I can see how density changes after every hour.
for k = 1:24
for i = 1:30
for j = 1:30
hrVal = zeros(1,1);
if ~isempty(gridPax{i,j})
hrVal = log(gridPax{i,j}(k)+1);
end
hrPax{i,j} = hrVal;
end
end
x = linspace(103.6,104,30);
y = linspace(1.5,1.25,30);
[X,Y] = meshgrid(x,y);
Z = cell2mat(hrPax);
% interpolate to a finer grid
newG = 200;
xq = linspace(103.6,104,newG);
xy = linspace(1.5,1.25,newG);
[Xq,Yq] = meshgrid(xq,xy);
Zq = interp2(X,Y,Z,Xq,Yq,'cubic');
figure('Name', 'Pax per Hour');
hold on;
surf(Xq,Yq,Zq);
xlabel 'Latitude'; ylabel 'Longitude'; zlabel 'No. of Pax';
axis tight
colormap default
end

채택된 답변

KSSV
KSSV 2017년 6월 19일
You place figure and hold on line outside the loop:
figure('Name', 'Pax per Hour');
hold on;
for k = 1:24
for i = 1:30
for j = 1:30
hrVal = zeros(1,1);
if ~isempty(gridPax{i,j})
hrVal = log(gridPax{i,j}(k)+1);
end
hrPax{i,j} = hrVal;
end
end
x = linspace(103.6,104,30);
y = linspace(1.5,1.25,30);
[X,Y] = meshgrid(x,y);
Z = cell2mat(hrPax);
% interpolate to a finer grid
newG = 200;
xq = linspace(103.6,104,newG);
xy = linspace(1.5,1.25,newG);
[Xq,Yq] = meshgrid(xq,xy);
Zq = interp2(X,Y,Z,Xq,Yq,'cubic');
surf(Xq,Yq,Zq);
xlabel 'Latitude'; ylabel 'Longitude'; zlabel 'No. of Pax';
axis tight
colormap default
drawnow
end
  댓글 수: 2
Atiqah Zakirah
Atiqah Zakirah 2017년 6월 19일
Thank you for pointing the problem! Code works but is there a way to slow down the transition to the next plot? Also, when I run it, the figure is displayed in 2D when it should be displayed in 3D without me having to use "Rotate 3D". Any idea why this is so?
KSSV
KSSV 2017년 6월 19일
Use pause() with specific time for time lapse....read about view to change to 3D view automatically.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by