Graphics - Backend, canvas, return_fig#

The graphic backend refers to the plotting library that is used for graphic output. Canvas refers to the frame/window/canvas/axes object the graphic output is forwarded to.

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/3770f73d0c6595b2a0888f03beab03d9d829d50d6c21b644e0433e2058e03eec.png
Plotting backend: 'plotly'