0

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)
    ims.append(im)

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

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)))

Thanks!

1 Answer 1

1

Using ArtistAnimation, all the lines are plotted on the same axes and are toggled visible/invisible at each frame, so they are all at the same scale. To get the desired result, I think you need to use FuncAnimation instead of ArtistAnimation.

Something like this (untested):

import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()

def animate(i):
    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']
    plt.cla()
    im = plt.plot(x, y)
    return im

ani = animation.FuncAnimation(fig, animate, frames=range(1,21), repeat=False, interval = 500)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.