I am trying to animate the y axis moving along the x axis while keeping it the same height the entire time. This works after a certain point, but the first couple of seconds the y axis scale gets larger, stretching the graph.

demo of giffing issue

In making this animation, I noticed that it automatically centers the plot. Is there a way to align the y axis with the left side of the frame (so that it looks like the x axis is sliding to the left)? This probably just comes down to specifying the size of the frame ahead of time, but I'm not sure how to do this.

Also, if you know how to get the red disk to keep from shrinking, that would be great but is not a priority.

Here's the code I have so far:

frame[t0_] := Show[Plot[f[t], {t, t0, 8}, AxesLabel -> {"t (s)","x (m)"}, AxesStyle -> Large, PlotRange -> {{t0 - 0.2, 8.2}, {-3, 5}}, AspectRatio -> 8/(8 - t0), Ticks -> {{0, 2, 4, 6, 8}, {-2, 0, 2, 4}}, ImageSize -> Medium], Graphics[{AspectRatio -> 1, Red, Disk[{t0, f[t0]}, 0.2]}]];

frames = Table[frame[t0], {t0, 0, 7.9, 0.1}];

Export["animation.gif", frames]

  • $\begingroup$ Why not just do a ~Manipulate~? $\endgroup$
    – Jagra
    Aug 26, 2022 at 18:02
  • $\begingroup$ Manipulate has the same issue with scaling the y axis. $\endgroup$
    – kajorge
    Aug 26, 2022 at 19:09

1 Answer 1


Change ImageSize to something like this:

ImageSize -> {550, 500}

When ImageSize is a single value, it defines the width. The aspect ratio started as wider-than-tall and then went to taller-than-wide, so just fixing the width still allowed the height to change for a bit. I picked {550, 500} arbitrarily, but you can probably compute it based on your other parameters. On that note, I suspect that what you actually wanted for AspectRatio was actually AspectRatio -> 8/(8.4 - t0) (or maybe I'm getting my dimensions mixed up), because the x-range is 8.4. However even that probably isn't right, because I don't think AspectRaio is for just the plot range, but the entire plot.

  • $\begingroup$ This perfectly fixed the scaling issue, thanks! And yes, I did mean to do 8.4 instead of 8. Last minute edit and it doesn't make a huge visual difference, but thanks for catching that. 😊 Now I just need to figure out how to keep it on the left side of the frame instead of automatically centering and I'll be all set! (I don't have enough reputation yet to upvote your answer, but it's exactly what I was looking for, thank you!) $\endgroup$
    – kajorge
    Aug 26, 2022 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.