1

Can anyone see what exactly is wrong with my code here? I am trying to generate an animated plot of my results to see its evolution over time and the result is just a full plot of my data, rather than an updating animation.

enter image description here

The sampleText.txt file is just a numpy array of size (5000, 1), The first 5 entries of the array are [[-0.01955058],[ 0.00658392[,[-0.00658371],[-0.0061325 ],[-0.0219136 ]]

import matplotlib.animation as animation
import time

fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot()

def animate(i):
    
    x = np.linspace(0, len(dataArray), len(dataArray))
    y = []

    for eachLine in x:
        y.append(eachLine * 10)
            
    ax1.clear()
    ax1.plot(x, y, color = 'red', alpha = 0.5)

ani = animation.FuncAnimation(fig, func = animate)

plt.show()

5
  • So what's the problem? You have half a question and half of a minimal reproducible example here Commented May 24, 2021 at 13:08
  • @MadPhysicist Please see my updates to the question. I have simplified the example and showed the issue more clearly. I get a plot outright of the entire data, rather than an animation of the updates which is what I want Commented May 24, 2021 at 13:28
  • Thanks for staying responsive. Could you provide a data sample. Like a call to np.arange or np.random, instead of just a verbal description? Commented May 24, 2021 at 13:53
  • I am not 100% sure what you mean but my this is an example of what the first 5 entries to my '(5000,1)' numpy array looks like '[[-0.01955058] [ 0.00658392] [-0.00658371] [-0.0061325 ] [-0.0219136 ]]' Commented May 24, 2021 at 13:58
  • You don't need the exact array. Just provide some code that generates something similar. See minimal reproducible example for reference Commented May 24, 2021 at 14:01

1 Answer 1

3

The basis of animation is a mechanism where the animation function sets the values to be changed for the initial graph setting. In this case, the line width is 3, the color is red, and x and y are an empty list. xy data is linked to the number of frames by i in the animation function.

from matplotlib import pyplot as plt 
import numpy as np 
from matplotlib.animation import FuncAnimation 

fig = plt.figure() 

# marking the x-axis and y-axis 
axis = plt.axes(xlim =(0, 500), ylim =(0, 1000)) 

x = np.linspace(0, 500, 500) 
y = x*2
# initializing a line variable 
line, = axis.plot([], [], lw=2, color='r') 

def animate(i): 
    line.set_data(x[:i], y[:i]) 
    return line, 

anim = FuncAnimation(fig, animate, frames=500, blit=True, repeat=False) 
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Please refer to the basics in the official reference. If my answer helped you, please consider accepting it as the correct answer
I will check this for my work and let you know. Thanks for the response.

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.