Backend and canvas#

Graphic backend#

Magpylib supports several common graphic backends.

from magpylib import SUPPORTED_PLOTTING_BACKENDS
SUPPORTED_PLOTTING_BACKENDS
('matplotlib', 'plotly', 'pyvista')

The installation default is Matplotlib. To select a graphic backend one can

  1. Change the library default with magpy.defaults.display.backend = 'plotly'.

  2. Set the backend kwarg in the show function, show(..., backend='matplotlib').

There is a high level of feature parity, however, not all graphic features are supported by all backends. In addition, some common Matplotlib syntax (e.g. color 'r', linestyle ':') is automatically translated to other backends.

The following example demonstrates the currently supported backends:

import numpy as np
import magpylib as magpy
import pyvista as pv

pv.set_jupyter_backend('panel') # improve rendering in a jupyter notebook

# define sources and paths
loop = magpy.current.Loop(current=1, diameter=1)
loop.position = np.linspace((0,0,-3), (0,0,3), 40)

cylinder = magpy.magnet.Cylinder(magnetization=(0,-100,0), dimension=(1,2), position=(0,-3,0))
cylinder.rotate_from_angax(np.linspace(0, 300, 40)[1:], 'z', anchor=0)

# show the system using different backends
for backend in magpy.SUPPORTED_PLOTTING_BACKENDS:
    print(f'Plotting backend: {backend!r}')
    magpy.show(loop, cylinder, backend=backend)
Plotting backend: 'matplotlib'
../_images/02028c1d30cad1465dd3839e340b38a4af7232ca31ed5ae408fafd8db4d5b72c.png
Plotting backend: 'plotly'