Graphics - Styles#

The graphic styles define how Magpylib objects are displayed visually when calling show. They can be fine-tuned and individualized in many ways.

There are multiple hierarchy levels that decide about the final graphical representation of the objects:

  1. When no input is given, the default style will be applied.

  2. Collections will override the color property of all children with their own color.

  3. Object individual styles will take precedence over these values.

  4. Setting a local style in show() will take precedence over all other settings.

Setting the default style#

The default style is stored in magpylib.defaults.display.style. Default styles can be set as properties,

magpy.defaults.display.style.magnet.magnetization.show = True
magpy.defaults.display.style.magnet.magnetization.color.middle = 'grey'
magpy.defaults.display.style.magnet.magnetization.color.mode = 'bicolor'

by assigning a style dictionary with equivalent keys,

magpy.defaults.display.style.magnet = {
    'magnetization': {'show': True, 'color': {'middle': 'grey', 'mode': 'tricolor'}}
}

or by making use of the update method:

magpy.defaults.display.style.magnet.magnetization.update(
    'show': True,
    'color': {'middle'='grey', mode='tricolor',}
)

All three examples result in the same default style.

Once modified, the library default can always be restored with the magpylib.style.reset() method. The following practical example demonstrates how to create and set a user defined magnetization style as default,

import magpylib as magpy
from magpylib.magnet import Cuboid, Cylinder, Sphere

cube = Cuboid(magnetization=(1, 0, 0), dimension=(1, 1, 1))
cylinder = Cylinder(magnetization=(0, 1, 0), dimension=(1, 1), position=(2,0,0))
sphere = Sphere(magnetization=(0, 1, 1), diameter=1, position=(4,0,0))

print('Default magnetization style')
magpy.show(cube, cylinder, sphere, backend="plotly")

user_defined_style = {
    'show': True,
    'color': {
        'transition': 1,
        'mode': 'tricolor',
        'middle': 'white',
        'north': 'magenta',
        'south': 'turquoise',
    },
}
magpy.defaults.display.style.magnet.magnetization = user_defined_style

print('Custom magnetization style')
magpy.show(cube, cylinder, sphere, backend="plotly")
Default magnetization style