Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Alexboiboi
Copy link
Member

@Alexboiboi Alexboiboi commented Jun 14, 2024

Notes

This PR aims to add a new field visualization method that easily integrates with Magpylib. The Sensor object with its pixel is the perfect object to use for this purpose!
With this new capability one can display the vector field for "B", "H", "M", or "J" and overlay a magnitude (e.g. "Bxy", "Hz", etc.. )

Below some example on how it may be used (more in the PR docs):

Animated B-field

Animation

Code to reproduce
import magpylib as magpy
import numpy as np

path_len=51
pix_per_dim = 12

c1 = magpy.magnet.Cuboid(polarization=(0, 0, 1), dimension=(1, 1, 1), style_opacity=0.2)
c1.rotate_from_angax(np.linspace(0, 180, path_len), "z", start=0)
c1.rotate_from_angax(np.linspace(0, 180, path_len), "x", start=0)

ls = np.linspace(-1, 1, pix_per_dim)
s1 = magpy.Sensor(pixel=[[x, y, 0] for x in ls for y in ls], position=(0, 0, 0))

magpy.show(
    c1,
    s1,
    animation=True,
    style_pixel_field_symbol="arrow3d",
    style_pixel_field_vectorsource="B",
)

Tasks

Updated Task List

  • Align style.pixel.field parameters naming and behavior

    • source (default=None): defines the field source for vector direction and color mapping:
      • None : No field representation is shown
      • "B", "Hxy", "Jxyz", etc.: Colors are mapped to the magnitude of the specified field
    • symbol (default="cone"): specifies the rendering symbol for field values
      • "none": pixel.symbol representation takes precedence over pixel.field.symbol
      • "cone": 3D cone representation
      • "arrow3d": 3D arrow representation
      • "arrow": 2D line-based arrow
    • shownull (default=True): toggles visibility of pixels with zero and invalid field vectors
      • True: Null vectors are displayed
      • False: Null vectors are hidden
    • sizescaling (default="uniform"): determines how arrow size relates to the source magnitude
      • "uniform": Uniform arrow size
      • "linear": Size proportional to magnitude
      • "log": Size proportional to normalized logarithm of magnitude
      • "log^n": Size proportional to normalized nth (2 to 9) logarithm of magnitude
    • sizemin (default=0.1): minimum relative size of field symbols (float between 0 and 1)
    • colorscaling (default="uniform"): determines how arrow color relates to the source magnitude
      • "uniform": Uniform color for all arrows
      • "linear": Color scaling proportional to magnitude
      • "log": Color scaling proportional to normalized logarithm of magnitude
      • "log^n": Color scaling proportional to normalized nth (2 to 9) logarithm of magnitude
    • colormap (default="Viridis"): specifies the colormap used for color mapping
      • Supports standard color maps (e.g., "Viridis", "Inferno", "Magma", etc.) compatible with both Plotly and Matplotlib
  • Magnitude normalization is performed individually for each sensor along its path

  • style.pixel.symbol behavior when field representation is disabled:

    • When pixel.field.source=None or pixel.field.symbol="none", falls back to pixel.symbol representation
    • Null or invalid vectors use default representation (point/box or according to pixel.symbol)
  • Show hull over pixels only when no field values are provided (when pixel.field.source=None)

  • Make sure it works with all backends

    • matplotlib
    • plotly
    • pyvista
  • Docs: add or modify an example to reflect the changes

  • Changelog: document new feature

  • Testing: add unit tests to cover the common and edge cases

@OrtnerMichael
Copy link
Member

OrtnerMichael commented Jul 22, 2025

After a long discussion with CoPilot

Its mostly telling me what it thinks I want to hear, and not offering so much objective perspective - but it helped me go through the use cases. I suggest the following interface:

style.sensor.pixel.field=None

  • allow None, "B", "Hxy", "Hx", etc..
  • If None all pixels are displayed the same (uniform mode), else we are in the variable mode where all pixel are displayed trying to visualize the given field

style.sensor.pixel.symbol="box" - sets the pixel shape in uniform mode

  • allow 2D marker like "o", "*", ... and 3D primitives like "box", "cone", ...

style.sensor.pixel.fieldsymbol="arrow3d" - sets the pixel shape in variable mode

  • allow 2D marker like "o", "*", ... and 3D primitives like "box", "cone", ...
  • Note: some symbols can display orientation, some cannot, the ones that cannot should not be oriented (like eg. "box")

style.sensor.pixel.size=1.0

  • allow all numbers >0.
  • Multiplicative factor on default pixel size which is not always optimal

style.sensor.pixel.sizescaling="uniform"

  • options: "uniform", "linear", "log", "loglog"
  • as already implemented

style.sensor.pixel.color='k'

  • allow any valid color "k", (.1,.2,.3), ...
  • sets the uniform mode pixel color

style.sensor.pixel.colormap='Viridis'

  • allow standard color maps "Jet", "RgYnBl", ...
  • sets the variable mode pixel color

style.sensor.pixel.colorscaling="linear"

  • options: "uniform", "linear", "log", "loglog"
  • when "uniform" display pixel in style.sensor.pixel.color

Alexboiboi and others added 24 commits September 14, 2025 11:20
…or improved clarity in Pixel Field documentation
@Alexboiboi
Copy link
Member Author

@OrtnerMichael Ready for final review!

As discussed in our latest meeting, we are still going to keep the nested structure with the following parameters:

  • source (default=None):
    Defines the field source of the vector field representation.

    • None: No field representation is shown
    • "B", "Hxy", "Jxyz", etc.: Colors are mapped to the magnitude of the specified field.
  • symbol (default="cone"):
    Specifies the rendering symbol for field values.

    • "none": pixel.symbol representation takes precedence of pixel.field.symbol.
    • "cone": 3D cone representation.
    • "arrow3d": 3D arrow representation.
    • "arrow": 2D line-based arrow.
  • shownull (default=True):
    Toggles the visibility of pixel with zero and invalid field vectors.

    • True: Null vectors are displayed.
    • False: Null vectors are hidden.
  • sizescaling (default="uniform"):
    Determines how arrow size relates to the source magnitude.

    • "uniform": Uniform arrow size.
    • "linear": Size proportional to magnitude.
    • "log": Size proportional to the normalized logarithm of the magnitude.
    • "log^n": Size proportional to the normalized nth (2 to 9) logarithm of the magnitude.
  • sizemin (default=0.1)
    Minimum relative size of field symbols. A float between 0 and 1.
    When displaying field vectors this controls how small the symbols
    can become relative to their maximum size. A value of 0 allows symbols to shrink to zero size,
    while 0.5 ensures symbols are at least 50% of their maximum size.

  • colorscaling (default="uniform"):
    Determines how arrow color relates to the source magnitude.

    • "uniform": Uniform color for all arrows.
    • "linear": Color scaling proportional to magnitude.
    • "log": Color scaling proportional to the normalized logarithm of the magnitude.
    • "log^n": Color scaling proportional to the normalized nth (2 to 9) logarithm of the magnitude.
  • colormap (default="Viridis"):
    Specifies the colormap used for color mapping. Supports standard color maps (e.g., "Viridis", "Inferno", "Magma", etc.) compatible with both Plotly and Matplotlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants