You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@ All notable changes to magpylib are documented here.
6
6
# Unreleased
7
7
* Field computation `getB`/`getH` now supports 2D [pandas](https://pandas.pydata.org/).[dataframe](https://pandas.pydata.org/docs/user_guide/dsintro.html#dataframe) in addition to the `numpy.ndarray` as output type. ([#523](https://github.com/magpylib/magpylib/pull/523))
8
8
* Internal `getB`/`getH` refactoring. Like for the object oriented interface, the direct interface for `'Line'` current now also accepts `'vertices'` as argument. ([#540](https://github.com/magpylib/magpylib/pull/540))
9
+
* Complete plotting backend rework to prepare for easy implementation of new backends, with minimal maintenance. ([#539](https://github.com/magpylib/magpylib/pull/539))
Copy file name to clipboardExpand all lines: docs/examples/examples_13_3d_models.md
+36-53Lines changed: 36 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,20 +18,20 @@ kernelspec:
18
18
(examples-own-3d-models)=
19
19
## Custom 3D models
20
20
21
-
Each Magpylib object has a default 3D representation that is displayed with `show`. Users can add a custom 3D model to any Magpylib object with help of the `style.model3d.add_trace` method. The new trace is stored in `style.model3d.data`. User-defined traces move with the object just like the default models do. The default trace can be hidden with the command `obj.model3d.showdefault=False`.
21
+
Each Magpylib object has a default 3D representation that is displayed with `show`. Users can add a custom 3D model to any Magpylib object with help of the `style.model3d.add_trace` method. The new trace is stored in `style.model3d.data`. User-defined traces move with the object just like the default models do. The default trace can be hidden with the command `obj.model3d.showdefault=False`. When using the `'generic'` backend, custom traces are automatically translated into any other backend. If a specific backend is used, it will only show when called with the corresponding backend.
22
22
23
23
The input `trace` is a dictionary which includes all necessary information for plotting or a `magpylib.graphics.Trace3d` object. A `trace` dictionary has the following keys:
24
24
25
-
1.`'backend'`: `'matplotlib'` or `'plotly'`
25
+
1.`'backend'`: `'generic'`, `'matplotlib'` or `'plotly'`
26
26
2.`'constructor'`: name of the plotting constructor from the respective backend, e.g. plotly `'Mesh3d'` or matplotlib `'plot_surface'`
27
27
3.`'args'`: default `None`, positional arguments handed to constructor
28
28
4.`'kwargs'`: default `None`, keyword arguments handed to constructor
29
-
5.`'coordsargs'`: tells magpylib which input corresponds to which coordinate direction, so that geometric representation becomes possible. By default `{'x': 'x', 'y': 'y', 'z': 'z'}` for the Plotly backend and `{'x': 'args[0]', 'y': 'args[1]', 'z': 'args[2]'}` for the Matplotlib backend.
29
+
5.`'coordsargs'`: tells magpylib which input corresponds to which coordinate direction, so that geometric representation becomes possible. By default `{'x': 'x', 'y': 'y', 'z': 'z'}` for the `'generic'` backend and Plotly backend, and `{'x': 'args[0]', 'y': 'args[1]', 'z': 'args[2]'}` for the Matplotlib backend.
30
30
6.`'show'`: default `True`, toggle if this trace should be displayed
**Matplotlib** plotting functions often use positional arguments for $(x,y,z)$ input, that are handed over from `args=(x,y,z)` in `trace`. The following examples show how to construct traces with `plot`, `plot_surface` and `plot_trisurf`:
Automatic trace generators are provided for several 3D models in `magpylib.graphics.model3d`. They can be used as follows,
168
+
Automatic trace generators are provided for several basic 3D models in `magpylib.graphics.model3d`. If no backend is specified, it defaults back to `'generic'`. They can be used as follows,
As shown in {ref}`examples-3d-models`, it is possible to attach custom 3D model representations to any Magpylib object. In the example below we show how a standard CAD model can be transformed into a Magpylib graphic trace, and displayed by both `matplotlib` and `plotly` backends.
235
+
As shown in {ref}`examples-3d-models`, it is possible to attach custom 3D model representations to any Magpylib object. In the example below we show how a standard CAD model can be transformed into a generic Magpylib graphic trace, and displayed by both `matplotlib` and `plotly` backends.
242
236
243
237
```{note}
244
238
The code below requires installation of the `numpy-stl` package.
@@ -251,18 +245,20 @@ import requests
251
245
import numpy as np
252
246
from stl import mesh # requires installation of numpy-stl
**{('opacity' if backend=='plotly' else 'alpha') :0.5}
85
+
opacity=0.5,
88
86
)
89
87
return trace
90
88
```
@@ -125,7 +123,6 @@ Custom traces can be computationally costly to construct. In the above example,
125
123
To make our compounds ready for heavy computation, it is possible to provide a callable as a trace, which will only be constructed when `show` is called. The following modification of the above example demonstrates this:
126
124
127
125
```{code-cell} ipython3
128
-
from functools import partial
129
126
import magpylib as magpy
130
127
import numpy as np
131
128
@@ -143,8 +140,7 @@ class MagnetRingAdv(magpy.Collection):
**{('opacity' if backend=='plotly' else 'alpha') :0.5}
185
+
opacity=0.5,
191
186
)
192
187
return trace
193
188
```
194
189
195
190
All we have done is, to remove the trace construction from the `_update` method, and instead provide `_custom_trace3d` as callable in `__init__` with the help of `partial`.
196
191
197
192
```{code-cell} ipython3
198
-
ring0 = MagnetRing()
193
+
ring0 = MagnetRing()
199
194
%time for _ in range(100): ring0.cubes=10
200
195
201
-
ring1 = MagnetRingAdv()
196
+
ring1 = MagnetRingAdv()
202
197
%time for _ in range(100): ring1.cubes=10
203
198
```
204
199
@@ -214,5 +209,3 @@ for i,cub in zip([2,7,12,17,22], [20,16,12,8,4]):
0 commit comments