Animations#

Magpylib can display the motion of objects along paths in the form of animations.

Hint

  1. Animations work best with the plotly backend.

  2. If your browser window opens, but your animation does not load, reload the page (ctrl+r in chrome).

  3. Avoid rendering too many frames.

Detailed information about how to tune animations can be found in the graphics documentation.

Simple Animations#

Animations are created with show by setting animation=True. It is also possible to hand over the animation time with this kwarg.

import numpy as np
import magpylib as magpy

# Define magnet with path
magnet = magpy.magnet.Cylinder(
    polarization=(1, 0, 0),
    dimension=(2, 1),
    position=(4,0,0),
    style_label="magnet",
)
magnet.rotate_from_angax(angle=np.linspace(0, 300, 40), start=0, axis="z", anchor=0)

# Define sensor with path
sensor = magpy.Sensor(
    pixel=[(-.2, 0, 0), (.2, 0, 0)],
    position = np.linspace((0, 0, -3), (0, 0, 3), 40),
    style_label="sensor",
)

# Display as animation - prefers plotly backend
magpy.show(sensor, magnet, animation=True, backend='plotly')

Animated Subplots#

Subplots are a powerful tool to see the field along a path while viewing the 3D models at the same time. This is specifically illustrative as an animation where the field at the respective path position is indicated by a marker.

# Continuation from above - ensure previous code is executed

magpy.show(
    dict(objects=[magnet, sensor], output=["Bx", "By", "Bz"], col=1),
    dict(objects=[magnet, sensor], output="model3d", col=2),
    backend='plotly',
    animation=True,
)

It is also possible to use the show_context context manager.

# Continuation from above - ensure previous code is executed

with magpy.show_context([magnet, sensor], backend='plotly', animation=True) as sc:
    sc.show(output="Bx", col=1, row=1)
    sc.show(output="By", col=1, row=2)
    sc.show(output="Bz", col=2, row=1)
    sc.show(output="model3d", col=2, row=2)