Graphics#

Once all Magpylib objects and their paths have been created, show provides a convenient way to graphically display the geometric arrangement using the Matplotlib (default) and Plotly packages. When show is called, it generates a new figure which is then automatically displayed.

The desired graphic backend is selected with the backend keyword argument. To bring the output to a given, user-defined figure, the canvas argument is used. This is demonstrated in Plotting backends.

The following example shows the graphical representation of various Magpylib objects and their paths using the default Matplotlib graphic backend.

import numpy as np
import pyvista as pv

import magpylib as magpy

objects = {
    "Cuboid": magpy.magnet.Cuboid(
        polarization=(0, -0.1, 0),
        dimension=(0.01, 0.01, 0.01),
        position=(-0.06, 0, 0),
    ),
    "Cylinder": magpy.magnet.Cylinder(
        polarization=(0, 0, 0.01),
        dimension=(0.01, 0.01),
        position=(-0.05, 0, 0),
    ),
    "CylinderSegment": magpy.magnet.CylinderSegment(
        polarization=(0, 0, 0.01),
        dimension=(0.003, 0.01, 0.01, 0, 140),
        position=(-0.03, 0, 0),
    ),
    "Sphere": magpy.magnet.Sphere(
        polarization=(0, 0, 0.01),
        diameter=0.01,
        position=(-0.01, 0, 0),
    ),
    "Tetrahedron": magpy.magnet.Tetrahedron(
        polarization=(0, 0, 0.01),
        vertices=((-0.01, 0, 0), (0.01, 0, 0), (0, -0.01, 0), (0, -0.01, -0.01)),
        position=(-0.04, 0, 0.04),
    ),
    "TriangularMesh": magpy.magnet.TriangularMesh.from_pyvista(
        polarization=(0, 0, 0.01),
        polydata=pv.Dodecahedron(radius=0.01),
        position=(-0.01, 0, 0.04),
    ),
    "Circle": magpy.current.Circle(
        current=1,
        diameter=0.01,
        position=(0.04, 0, 0),
    ),
    "Polyline": magpy.current.Polyline(
        current=1,
        vertices=[
            (0.01, 0, 0),
            (0, 0.01, 0),
            (-0.01, 0, 0),
            (0, -0.01, 0),
            (0.01, 0, 0),
        ],
        position=(0.01, 0, 0),
    ),
    "Dipole": magpy.misc.Dipole(
        moment=(0, 0, 1),
        position=(0.03, 0, 0),
    ),
    "Triangle": magpy.misc.Triangle(
        polarization=(0, 0, 0.01),
        vertices=((-0.01, 0, 0), (0.01, 0, 0), (0, 0.01, 0)),
        position=(0.02, 0, 0.04),
    ),
    "Sensor": magpy.Sensor(
        pixel=[(0, 0, z) for z in (-0.005, 0, 0.005)],
        position=(0, -0.03, 0),
    ),
}

objects["Circle"].move(np.linspace((0, 0, 0), (0, 0, 0.05), 20))
objects["Cuboid"].rotate_from_angax(np.linspace(0, 90, 20), "z", anchor=0)

magpy.show(*objects.values())