2

The code is shown below. I am attempting to animate using vectors calculated earlier a figure window is opened so i know it gets this far and the vectors are being calclated correctly. But matplotlib oututs nothing but the figure window I have no idea why. Please help.

#finally animateing
fig = plt.figure()
ax = plt.axes(xlim = (-1000,1000) ,ylim = (-1000,1000))#limits were arbitrary
#line = ax.plot([],[])
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,


def animate(i):
 x = time_vec[i]
 y = complex_vec[i]
 #y1 = real_vec[i] 
 #y2 = modulus_vec[i]
 line.set_data(x,y)
 #line.set_data(x,y1)
 #line.set_data(x,y2) 
 return line,

animation_object = animation.FuncAnimation(fig, animate, init_func= init, frames = num_files,interval = 30, blit = True)

#turnn this line on to save as mp4
#anim.save("give it a name.mp4", fps = 30, extra-args = ['vcodec', 'libx264'])
plt.show()

THE FULL ERROR MESSAGE IS SHOWN BELOW

    Traceback (most recent call last):
  File "the_animation.py", line 71, in <module>
    plt.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 145,     in show
    _show(*args, **kw)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py",     line 117, in __call__
    self.mainloop()
  File     "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line     69, in mainloop
    Tk.mainloop()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 366, in mainloop
    _default_root.tk.mainloop(n)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1484, in __call__
    def __call__(self, *args):

MINIMAL EXAMPLE

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
complex_vec = np.arange(5,6,.001)
real_vec = np.arange(7,8,.001)
time_vec = np.arange(0,1,.001)
num_files = np.size(time_vec)
#creating the modulus vector
modulus_vec = np.zeros(np.shape(complex_vec))
for k in range (0,complex_vec.size):
    a = complex_vec[k]
    b = real_vec[k]
    calc_modulus = np.sqrt(a**2 + b**2)
    modulus_vec[k] = calc_modulus
#finally animateing
fig = plt.figure()
ax = plt.axes(xlim = (-1000,1000) ,ylim = (-1000,1000))#limits were     arbitrary
#line = ax.plot([],[])
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,


def animate(i):
    x = time_vec[i]
    y = complex_vec[i]
    y1 = real_vec[i] 
    y2 = modulus_vec[i]
    line.set_data(x,y)
    line.set_data(x,y1)
    line.set_data(x,y2) 
    return line,

animation_object = animation.FuncAnimation(fig, animate, init_func= init, frames = num_files,interval = 30, blit = True)

#turnn this line on to save as mp4
#anim.save("give it a name.mp4", fps = 30, extra-args = ['vcodec',     'libx264'])
plt.show()
7
  • what line are you receiving the error your title reads on? Commented Jul 26, 2015 at 21:32
  • thats the even weirder part i will post the erroro message in just a moment Commented Jul 26, 2015 at 21:34
  • @Ioma you sure that's the full error? Because your error output has no error Commented Jul 26, 2015 at 21:48
  • you are right i suppossed that because nothing was showing up i would get an error but this just pops up so i assumed it was an error but I guess not however I am realy confused now because even after running for 2 hrs it still does not show anything just the figure window no axes nothing Commented Jul 26, 2015 at 21:51
  • @Ioma do you have some sort of minimal example you can post or link to this file if it's a single script? it's difficult to debug with no solid error output or access to the file Commented Jul 26, 2015 at 21:54

1 Answer 1

1

The problem here is in your animate function, you're using set_data multiple times which does not do what you think it does. You're using it like an append, when it's a set. The arguments should be two arrays, containing the respective x and y values for that line. This will animate your minimal example:

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

complex_vec = np.arange(5,6,.001)
real_vec = np.arange(7,8,.001)
time_vec = np.arange(0,1,.001)
num_files = np.size(time_vec)

#creating the modulus vector
modulus_vec = np.zeros(np.shape(complex_vec))
for k in range (0,complex_vec.size):
    a = complex_vec[k]
    b = real_vec[k]
    calc_modulus = np.sqrt(a**2 + b**2)
    modulus_vec[k] = calc_modulus

#finally animateing
fig = plt.figure()
ax = plt.axes(xlim = (-1,1) ,ylim = (-1,15))#limits were     arbitrary
#line = ax.plot([],[])
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = time_vec[i]
    y = complex_vec[i]
    y1 = real_vec[i] 
    y2 = modulus_vec[i]
    # notice we are only calling set_data once, and bundling the y values into an array
    line.set_data(x,np.array([y, y1, y2]))
    return line,

animation_object = animation.FuncAnimation(fig, 
                                           animate, 
                                           init_func= init, 
                                           frames = num_files,
                                           interval = 30, 
                                           blit = True)

#turnn this line on to save as mp4
#anim.save("give it a name.mp4", fps = 30, extra-args = ['vcodec',     'libx264'])
plt.show()

Your previous attempt was setting the x and y values, then overwriting the previous with a new x and y, then doing that once again.

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.