Skip to main content

magpylib

Actions Status Documentation Status

PyPI version Conda-Forge PyPI platforms

GitHub Discussion

Coverage

[!WARNING] Version 5 introduces critical breaking changes with, among others, the move to SI units. We recommended to pin your dependencies to magpylib>=4.5<5 until you are ready to migrate to the latest version! (see details)

Magpylib is an open-source Python package for calculating static magnetic fields of magnets, currents, and other sources. It uses analytical expressions, solutions to macroscopic magnetostatic problems, implemented in vectorized form which makes the computation extremely fast and leverages the open-source Python ecosystem for spectacular visualizations!

Installation

Install from PyPI using pip

pip install magpylib

Install from conda forge using conda

conda install -c conda-forge magpylib

Magpylib supports Python3.11+ and relies on common scientific computation libraries NumPy, Scipy, Matplotlib and Plotly. Optionally, Pyvista is recommended as graphical backend.

Resources

  • Check out our Documentation for detailed information about the last stable release, or the Dev Docs to see the unreleased development version features.
  • Please abide by our Code of Conduct.
  • Contribute through Discussions and coding by following the Contribution Guide. The Git project Issues give an up-to-date list of potential enhancements and planned milestones. Propose new ones.
  • A Youtube video introduction to Magpylib v4.0.0 within the GSC network.
  • An open-access paper from the year 2020 describes v2 of this library with most basic concepts still intact in later versions.

Quickstart

Here is an example on how to use Magpylib.

import magpylib as magpy

# Create a Cuboid magnet with sides 1, 2 and 3 cm respectively, and a polarization
# of 1000 mT pointing in x-direction.
cube = magpy.magnet.Cuboid(
    polarization=(1, 0, 0),  # in SI Units (T)
    dimension=(0.01, 0.02, 0.03),  # in SI Units (m)
)

# By default, the magnet position is (0, 0, 0) and its orientation is the unit
# rotation (given by a scipy rotation object), which corresponds to magnet sided
# parallel to global coordinate axes.
print(cube.position)  # --> [0. 0. 0.]
print(cube.orientation.as_rotvec())  # --> [0. 0. 0.]

# Manipulate object position and orientation through the respective attributes,
# or by using the powerful `move()` and `rotate()` methods.
cube.move((0, 0, -0.02))  # in SI Units (m)
cube.rotate_from_angax(angle=45, axis="z")
print(cube.position)  # --> [0. 0. -0.02]
print(cube.orientation.as_rotvec(degrees=True))  # --> [0. 0. 45.]

# Compute the magnetic B-field in units of T at a set of observer positions. Magpylib
# makes use of vectorized computation. Hand over all field computation instances,
# e.g. different observer positions, at one function call. Avoid Python loops !!!
observers = [(0, 0, 0), (0.01, 0, 0), (0.02, 0, 0)]  # in SI Units (m)
B = magpy.getB(cube, observers)
print(B.round(2))  # --> [[-0.09 -0.09  0.  ]
#                         [ 0.   -0.04  0.08]
#                         [ 0.02 -0.01  0.03]]  # in SI Units (T)

# Sensors are observer objects that can have their own position and orientation.
# Compute the H-field in units of A/m.
sensor = magpy.Sensor(position=(0, 0, 0))
sensor.rotate_from_angax(angle=45, axis=(1, 1, 1))
H = magpy.getH(cube, sensor)
print(H.round())  # --> [-94537. -35642. -14085.]  # in SI Units (A/m)

# Position and orientation attributes of Magpylib objects can be vectors of
# multiple positions/orientations referred to as "paths". When computing the
# magnetic field of an object with a path, it is computed at every path index.
cube.position = [(0, 0, -0.02), (1, 0, -0.02), (2, 0, -0.02)]  # in SI Units (m)
B = cube.getB(sensor)
print(B.round(2))  # --> [[-0.12 -0.04 -0.02]
#                         [ 0.   -0.    0.  ]
#                         [ 0.   -0.    0.  ]] # in SI Units (T)

# When several objects are involved and things are getting complex, make use of
# the `show()` function to view your system through Matplotlib, Plotly or Pyvista backends.
magpy.show(cube, sensor, backend="pyvista")

More details and other important features are described in detail in the Documentation. Key features are:

  • Collections: Group multiple objects for common manipulation
  • Complex shapes: Create magnets with arbitrary shapes
  • Graphics: Styling options, graphic backends, animations, and 3D models
  • CustomSource: Integrate your own field implementation
  • Direct interface: Bypass the object oriented interface (max speed)

How can I cite this library ?

We would be happy if you give us credit for our efforts. A valid bibtex entry for the 2020 open-access paper would be

@article{ortner2020magpylib,
  title={Magpylib: A free Python package for magnetic field computation},
  author={Ortner, Michael and Bandeira, Lucas Gabriel Coliado},
  journal={SoftwareX},
  volume={11},
  pages={100466},
  year={2020},
  publisher={Elsevier}
}

A valid software citation could be

@software{magpylib,
    author = {{Michael-Ortner et al.}},
    title = {magpylib},
    url = {https://magpylib.readthedocs.io/en/latest/},
    version = {5.2.2},
    date = {2023-06-25},
}

Release files for magpylib 5.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for magpylib 5.2.3
File Size Uploaded
magpylib-5.2.3.tar.gz 16.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for magpylib 5.2.3
File Interpreter ABI Platform
magpylib-5.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 MB

Release files / magpylib-5.2.3.tar.gz

Download URL magpylib-5.2.3.tar.gz
Size 16.0 MB
Tags Source
SHA-256 checksum
How to use checksums
0d673306b1bbf8f8d36d9ba11ef1731341cd0a17126494d872b7f6ccb207fad1
BLAKE2b-256 checksum
How to use checksums
5bc960652c1af5c0316a4b6fd861219df4366c271b73d90a6629a336a48c37e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 16, 2026.

Transparency log

Release files / magpylib-5.2.3-py3-none-any.whl

Download URL magpylib-5.2.3-py3-none-any.whl
Size 233.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4d1bb2d50357a3c2b73b96aae2599d300dd970fd59a8ef64ef20ee86b9c71cd
BLAKE2b-256 checksum
How to use checksums
fc4ceb27ffc52aed60b04328d86d1222d9875055cc1b424cd40b93ef845bc764
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

5.2.3 This release

2 release files

5.2.2

2 release files

5.2.1

2 release files

5.2.0

2 release files

5.1.1

2 release files

5.1.0

2 release files

5.0.4

2 release files

5.0.3

2 release files

5.0.2

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.5.1

2 release files

4.5.0

2 release files

4.4.1

2 release files

4.4.0

2 release files

4.3.0

2 release files

4.2.0

2 release files

4.1.2

1 release file

4.1.1

1 release file

4.1.0

1 release file

4.0.4

1 release file

4.0.3

1 release file

4.0.2

1 release file

4.0.1

1 release file

4.0.0

1 release file

3.0.5

1 release file

3.0.4

1 release file

3.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page