Graphics - Backend, canvas, return_fig
Contents
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
Change the library default with
magpy.defaults.display.backend = 'plotly'
.Set the
backend
kwarg in theshow
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'

Plotting backend: 'plotly'