ICantHandleThis - 2 months ago 10x

Python Question

I'm learning how to animate in python for one of my projects and I'm basing my code off of the following example from here.

My adaption of their code goes as follows:

`import numpy as np`

import h5py, os, glob, sys, time

import matplotlib as mpl

mpl.use('Agg')

import matplotlib.pyplot as plt

from matplotlib.animation import FuncAnimation

def update(i):

for j in np.arange(0,10):

for k in np.arange(0,10):

for channel in ["N","E"]:

x = some_x_value

y = some_y_value

line = plt.loglog(x,y)

ax.set_xlabel(label)

return line, ax

if __name__ == "__main__":

fig, ax = plt.subplots()

anim = FuncAnimation(fig, update, frames=np.arange(0,10), interval=200)

anim.save('Test.gif', dpi=80, writer='imagemagick')

And when I try to run my script I get the following error:

Name Error: name 'update' is not defined.

As I said before, I'm still learning how to animate and don't understand all of what's going on in the code tutorial I found. However, I'm very confused as to why update isn't recognized at all as the way I call update seems to be exactly the same as what's in the tutorial.

Answer

```
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def update(i, ln):
i = i+1
x = i
y = i ** 2
x_data = ln.get_xdata()
y_data = ln.get_ydata()
ln.set_data(np.concatenate(([x], x_data)),
np.concatenate(([y], y_data)))
return ln
if __name__ == "__main__":
fig, ax = plt.subplots()
ax.set_xlim(1, 10)
ax.set_ylim(1, 100)
line, = ax.loglog([1], [1])
anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200,
fargs=(line, ))
anim.save('Test.gif', dpi=80, writer='imagemagick')
```

Works as expected. This makes me think that there is some other error in your code which is getting masked.

Source (Stackoverflow)

Comments