Animating pcolor plots to show evolution?
14 views (last 30 days)
Show older comments
Is there a way to animate a pcolor plot? Like have the y axis gradually fill with my simulation data?
0 Comments
Answers (1)
Abhas
on 23 May 2024
Yes, you can animate a pcolor plot in MATLAB to gradually fill the y-axis with your simulation data by using a loop to update the data in the pcolor plot and then capturing each frame for the animation. The drawnow function is used to update the plot in each iteration of the loop, and the getframe function can capture each frame if you want to create a video file.
Here's an example MATLAB code to illustrate it:
x = linspace(0, 2*pi, 100); % x-axis
t = linspace(0, 4*pi, 200); % Time or y-axis
[X, T] = meshgrid(x, t);
Z = sin(X - T); % Some data to plot
% Prepare the figure
figure;
colormap('jet'); % Set the colormap
for i = 2:length(t) % Start from 2 to ensure we have at least a 2x2 matrix
% Select data up to current time, ensuring at least 2 rows for pcolor
X_slice = X(1:i, :);
T_slice = T(1:i, :);
Z_slice = Z(1:i, :);
% Update pcolor plot
pcolor(X_slice, T_slice, Z_slice); % Use sliced data
shading interp; % Use interpolated shading for smooth color transition
% Axis labels and title
xlabel('X');
ylabel('Time');
title('Animating pcolor Plot');
% Fix the color limits for consistency across frames
caxis([-1 1]);
% Fix the axes limits to the full dataset to avoid rescaling
xlim([min(x) max(x)]);
ylim([min(t) max(t)]);
% Update the plot
drawnow;
end
The above code creates a basic animation of a pcolor plot where the y-axis (representing time in this case) fills up gradually with data. The loop iterates over the time steps, updating the plot each time with the data available up to that point.
You may refer to the following documentation links to have a better understanding on pcolo and drawnow:
0 Comments
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!