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

Skip to content

Conversation

Alexboiboi
Copy link
Member

@Alexboiboi Alexboiboi commented Jun 23, 2022

Motivation

Implementing a new plotting backend is a tedious task, since it demands to rewrite plotting functions for all objects with all their style attributes. Also the collection logic has to be implemented every time.

New Approach

In this PR, a new approach is considered. In this case a 'generic' backend is defined, heavily based on the plotly trace syntax. The generic backend function returns a list of traces (as dict) with declarative properties, hence no need for knowledge of the internal magpylib logic of style, path, collections. The only thing to implement is a translator for each new backend, which is in most cases fairly straight forward and requires very little additional code.
One more advantage, is that for every new object of the the magpylib library, only one graphic implementation needs to be implemented and backends don't need to be updated.

Implementation

For plotting only two types of traces are really necessary: 'scatter3d' and 'mesh3d'
For a simple example the generic traces looks as follows:

{'type': 'mesh3d',
 'i': array([0, 0, 1, 2]),
 'j': array([1, 1, 2, 0]),
 'k': array([2, 3, 3, 3]),
 'x': array([ 1.,  0., -1.,  1.]),
 'y': array([ 0.,  1.,  0., -1.]),
 'z': array([-0.70710678,  0.70710678, -0.70710678,  0.70710678]),
 'colorscale': ((0.0, '#00b050'),
  (0.16, '#00b050'),
  (0.26, '#dddddd'),
  (0.74, '#dddddd'),
  (0.8400000000000001, '#e71111'),
  (1.0, '#e71111')),
 'intensity': array([0.5, 1. , 0.5, 0. ]),
 'showscale': False,
 'name': 'Tetrahedron',
 'color': '#2E91E5',
 'opacity': 1,
 'legendgroup': 'Tetrahedron(id=140663769004688)',
 'showlegend': True}

{'type': 'scatter3d',
 'mode': 'lines',
 'line_color': '#2E91E5',
 'opacity': 1,
 'x': array([0.25, 0.25, 0.13, 0.25, 0.37, 0.25, 0.25,  nan]),
 'y': array([0.        , 1.99999998, 1.79999998, 1.99999998, 1.79999998,
        1.99999998, 1.99999998,        nan]),
 'z': array([ 0.,  0.,  0.,  0.,  0.,  0.,  0., nan]),
 'legendgroup': 'Tetrahedron(id=140663769004688)',
 'showlegend': False}

Tasks

  • implement generic backend
  • matplotlib translator
  • plotly translator
  • solve custom model3d handling (custom extra traces attached to any magpylib object now become obsolete)
  • deal with magic syntax in generic trace to avoid special cases in every translator (e.g. line_width=2 vs line=dict(width=2))
  • update changelog
  • update docs
  • update tests

Notes

image

@Alexboiboi Alexboiboi marked this pull request as ready for review July 26, 2022 11:49
@Alexboiboi Alexboiboi requested a review from OrtnerMichael July 26, 2022 11:49
Copy link
Member

@OrtnerMichael OrtnerMichael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@OrtnerMichael OrtnerMichael merged commit 3e4fdf3 into main Jul 26, 2022
@OrtnerMichael OrtnerMichael deleted the display-backend-rework branch July 26, 2022 13:38
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