Ahmad AL-FIFI
Ahmad AL-FIFI

Reputation: 21

__init__() got an unexpected keyword argument 'extra_args'`

I am trying to create an animation for particle Trajectory in magnetic and Electric fields.

I reached a dead end with the below code. Getting an error

MovieWriter ffmpeg unavailable; using Pillow instead. Traceback (most recent call last): File "C:\Users\rexal.LAPTOP-T90ADT0B\PycharmProjects\pythonProject\Q2.py", line 94, in display_animation(anim) File "C:\Users\rexal.LAPTOP-T90ADT0B\PycharmProjects\pythonProject\Q2.py", line 48, in display_animation return HTML(anim_to_html(anim)) File "C:\Users\rexal.LAPTOP-T90ADT0B\PycharmProjects\pythonProject\Q2.py", line 59, in anim_to_html anim.save(f.name, fps=20, extra_args=['-vcodec', 'libx264', '-pix_fmt', 'yuv420p']) File "C:\Users\rexal.LAPTOP-T90ADT0B\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\animation.py", line 1117, in save writer = writer_cls(fps, **writer_kwargs) TypeError: init() got an unexpected keyword argument 'extra_args'

import numpy as np
import matplotlib.pyplot as p
from scipy.integrate import ode
from IPython.display import HTML
from tempfile import NamedTemporaryFile
import base64
from matplotlib import animation





def newton(t, Y, q, m, B, E):
    """Computes the derivative of the state vector y according to the equation of motion:
    Y is the state vector (x, y, z, u, v, w) === (position, velocity).
    returns dY/dt.
    """
    x, y, z = Y[0], Y[1], Y[2]
    u, v, w = Y[3], Y[4], Y[5]

    alpha = q / m
    return np.array([u, v, w, 0, alpha * B * w + E, -alpha * B * v])
#-------------------------------------

r = ode(newton).set_integrator('dopri5')


t0 = 0
x0 = np.array([0, 0, 0])
v0 = np.array([1, 1, 0])
initial_conditions = np.concatenate((x0, v0))

r.set_initial_value(initial_conditions, t0).set_f_params(1.0, 1.0, 1.0, 10.)
positions = []
t1 = 50
dt = 0.05
while r.successful() and r.t < t1:
    r.integrate(r.t+dt)
    positions.append(r.y[:3])

positions = np.array(positions)


#----------------------------------

def display_animation(anim):
    p.close(anim._fig)
    return HTML(anim_to_html(anim))



VIDEO_TAG = """<video controls>
 <source src="data:video/x-m4v;base64,{0}" type="video/mp4">
 Your browser does not support the video tag.
</video>"""
def anim_to_html(anim):
    if not hasattr(anim, '_encoded_video'):
        f = NamedTemporaryFile(suffix='.mp4', delete=False)
        anim.save(f.name, fps=20, extra_args=['-vcodec', 'libx264', '-pix_fmt', 'yuv420p'])
        f.flush()
        video = open(f.name, "rb").read()
        f.close()
        anim._encoded_video = base64.b64encode(video).decode('utf-8')

    return VIDEO_TAG.format(anim._encoded_video)
#-------------------------------------------------
FRAMES = 50
fig = p.figure()
ax = fig.add_subplot(111, projection='3d')

def init():
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')



# animation function.  This is called sequentially
def animate(i):
    current_index = int(positions.shape[0] / FRAMES * i)
    ax.cla()
    ax.plot3D(positions[:current_index, 0],
              positions[:current_index, 1],
              positions[:current_index, 2])
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')

# call the animator.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=FRAMES, interval=100)

# call our new function to display the animation
display_animation(anim)

Upvotes: 2

Views: 2618

Answers (1)

one
one

Reputation: 2585

Installing ffmpeg by:

conda install -c conda-forge av

solved my problem.

Upvotes: 1

Related Questions