Clear Filters
Clear Filters

How to prevent plots from resizing in making GIFs

12 views (last 30 days)
I am trying to implement two plots changing side-by-side as a GIF, but I'm noticing that the plots keep changing size and move around which makes the animation a bit jarring. Does anyone know what I could do to fix this?
Here is the code I'm using to generate the GIFs (the entire code is rather long so this is just the portion dedicated to plotting):
I initialize (outside the for loop) with
figure
subplot(1,2,1)
hold on
p1 = plot(dom_plot,u_sol, 'r');
xlim([-pi pi])
axis square
drawnow
subplot(1,2,2)
hold on
plot(k, norm(uv), 'b.', 'markersize', 10)
axis square
drawnow
And then within the for loop I update it as:
subplot(1,2,1)
p1 = plot(dom_plot, real(u_sol), 'r');
cla(p1)
axis([-pi, pi, -max_plot, max_plot])
drawnow
axis square
subplot(1,2,2)
plot(uv_0(end), norm(uv_0(1:end-1)), 'b.', 'markersize', 10)
drawnow
axis square
exportgraphics(gcf,'w_4_2_animation.gif','Append',true);
Below is an example of what one of these animations looks like. Any help would be much appreciated; thank you!

Answers (1)

DGM
DGM on 7 May 2024
Edited: DGM on 7 May 2024
Figure out the appropriate range of x and y limits you need, and then explicitly set them using xlim() and ylim().
  2 Comments
Marco
Marco on 7 May 2024
Hello, yes I am aware of xlim() and ylim() (I used axis() for the plot on the left which combines these) but for the plot on the right, I want to keep it so that the x and y limits keep updating every iteration. I just want the physical size of the plots to remain the same, if this is possible.
DGM
DGM on 8 May 2024
If you want to allow the limits (and tick labels) to change in the loop, you'll have to make room for them in order to keep the plot boxes fixed in one place. Use your loop and inspect the 'position' property of the axes in order to find something that will allow room for the tick labels in all cases. Then explicitly set that in the loop so that it stays fixed.

Sign in to comment.

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!