Backend and canvas#

Graphic backend#

Magpylib supports Matplotlib and Plotly as possible graphic backends. If a backend is not specified, the library default stored in magpy.defaults.display.backend will be used. The value can bei either 'matplotlib' or 'plotly'.

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 between the two backends but there are also some key differences, e.g. when displaying magnetization of an object. In addition, some common Matplotlib syntax (e.g. color 'r', linestyle ':') is automatically translated to Plotly and vice versa.

The following example shows first Matplotlib and then Plotly output:

import numpy as np
import magpylib as magpy

# 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)

# display the system with both backends, cylinder), cylinder, backend='plotly')