Updating y axis ranges for each plot in matplotlib animation

I have 20 files that I'd like to plot as the one shown below and then show one after another, resembling an animation. One given plot looks the one shown in the figure. enter image description here

The 20 plots have different ranges for their y axis. So, while the one shown in the figure above ranges from -184000 to -176000, another might range from -160000 to -170000. Keeping the axis constant in the range of the minimum and maximum of all graphs causes the plots to be too stretched out or shrunken.

I've written the following code:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(1, 21):
    file_out = "out" + "_" + str(i)
    outfile = pd.read_table(file_out, \
    skip_blank_lines=True, skipinitialspace=True, sep='\s+')
    x = outfile['col1']
    y = outfile['col2']
    im = plt.plot(x, y)

ani = animation.ArtistAnimation(fig, ims, repeat=False, interval = 500)

Is there a way to change the axis ranges for each new graph in the animation? I tried adding the following line to the code but I was not successful: plt.axes(xlim = (0, 100), ylim = (min(y), max(y)))


Read more here: https://stackoverflow.com/questions/64901191/updating-y-axis-ranges-for-each-plot-in-matplotlib-animation

Content Attribution

This content was originally published by Homap at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: