Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show and fig underscore magic to magpylib.show(...) and style.legend.show property #657

Merged
merged 15 commits into from
Aug 23, 2023

Conversation

Alexboiboi
Copy link
Member

@Alexboiboi Alexboiboi commented Aug 17, 2023

Related Issues

Notes

fig and show kwargs magic

In the same vein as the style magic:

  • kwargs with fig prefix are forwarded to the figure constructor (Figure for mpl and plotly, Plotter for pyvista)
  • kwargs with show prefix are forwarded to the show call (for all backends)
  • if fig or show are prefixed with a backend name, the kwargs will only be forwarded to the specific backend.

Here an example below with increasing order of precedence:

import magpylib as magpy

c = magpy.magnet.Cuboid((0, 0, 1000), (1, 1, 1))
c.show(
    fig=dict(layout_title_text="fig"),
    fig_layout_title_text="fig_layout",
    plotly_fig=dict(layout_title_text="plotly_fig"),
    plotly_fig_layout_title_text="plotly_fig_layout",
    backend="plotly",
)

legend style

For the legend I introduced it at the object level as style.legend.show. The advantage is, that you one can turn off specific traces at will.

magpy.show(..., style_legend_show=False) # hides all legends

@codecov
Copy link

codecov bot commented Aug 17, 2023

Codecov Report

Merging #657 (9021fd0) into main (2169f6d) will increase coverage by 0.03%.
The diff coverage is 99.56%.

@@            Coverage Diff             @@
##             main     #657      +/-   ##
==========================================
+ Coverage   99.22%   99.26%   +0.03%     
==========================================
  Files          55       55              
  Lines        7000     7073      +73     
==========================================
+ Hits         6946     7021      +75     
+ Misses         54       52       -2     
Files Changed Coverage Δ
magpylib/_src/defaults/defaults_values.py 100.00% <ø> (ø)
magpylib/_src/style.py 99.73% <94.44%> (-0.13%) ⬇️
magpylib/_src/display/backend_matplotlib.py 100.00% <100.00%> (ø)
magpylib/_src/display/backend_plotly.py 100.00% <100.00%> (ø)
magpylib/_src/display/backend_pyvista.py 83.33% <100.00%> (+1.78%) ⬆️
magpylib/_src/display/display.py 100.00% <100.00%> (ø)
magpylib/_src/display/traces_core.py 100.00% <100.00%> (ø)
magpylib/_src/display/traces_generic.py 100.00% <100.00%> (ø)
magpylib/_src/display/traces_utility.py 100.00% <100.00%> (+0.24%) ⬆️
magpylib/_src/obj_classes/class_Collection.py 100.00% <100.00%> (ø)
... and 11 more

magpylib/_src/obj_classes/class_current_Line.py Dismissed Show dismissed Hide dismissed
magpylib/_src/obj_classes/class_current_Loop.py Dismissed Show dismissed Hide dismissed
magpylib/_src/obj_classes/class_magnet_Cuboid.py Dismissed Show dismissed Hide dismissed
magpylib/_src/obj_classes/class_magnet_Cylinder.py Dismissed Show dismissed Hide dismissed
magpylib/_src/obj_classes/class_magnet_CylinderSegment.py Dismissed Show dismissed Hide dismissed
magpylib/_src/obj_classes/class_magnet_Sphere.py Dismissed Show dismissed Hide dismissed
magpylib/_src/obj_classes/class_misc_Dipole.py Dismissed Show dismissed Hide dismissed
magpylib/_src/display/traces_core.py Dismissed Show dismissed Hide dismissed
magpylib/_src/display/traces_generic.py Dismissed Show dismissed Hide dismissed
@Alexboiboi
Copy link
Member Author

One example to test the legends

import magpylib as magpy
import numpy as np

objs = []
f=0.5
N = 3
xs = f * np.array([-1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1])
ys = f * np.array([-1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1])
zs = f * np.array([-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1])
trace_mpl = dict(
    backend="matplotlib",
    constructor="plot",
    args=(xs, ys, zs),
    kwargs={"marker": "", "lw": 1},
)
trace_plotly = dict(
    backend="plotly",
    constructor="scatter3d",
    kwargs={"x": xs, "y": ys, "z": zs, "mode": "lines"},
)
trace_generic = dict(
    backend="generic",
    constructor="scatter3d",
    kwargs={"x": xs, "y": ys, "z": zs, "mode": "lines"},
)
objs.append(magpy.misc.CustomSource(style_label="Matplotlib extra trace"))
objs[-1].style.model3d.add_trace(trace_mpl)

objs.append(magpy.magnet.Cuboid((0,0,1000), [1]*3, style_label="Plotly extra trace"))
objs[-1].style.model3d.add_trace(trace_plotly)

objs.append(magpy.magnet.Cuboid((0,0,1000), [1]*3, style_label="Generic extra trace"))
objs[-1].style.model3d.add_trace(trace_generic)

for i, obj in enumerate(objs):
    obj.rotate_from_angax([10 * i for i in range(N)], "y", start=0, anchor=(0, 0, 10))
    obj.move((0,2*i,0))
for backend in ("matplotlib", "plotly"):
    magpy.show(
        *objs,
        backend=backend,
        style_path_frames=2,
        #style_legend_show=False,
        #style_model3d_showdefault=False,
    )

@Alexboiboi
Copy link
Member Author

@OrtnerMichael ready for review

@OrtnerMichael
Copy link
Member

hi @Alexboiboi


style_legend_show=True/False works nicely !!! HUGE fan of this !!!

Question: do we have a format option for legend display style ? like we have it in describe ?


show commands I don't understand where kwargs are sent. there is also no example.


fig commands:

  1. Works beautifully with Plotly

  2. Not very helpful with Matplotlib because all the styling happens at Axes level. At Figure level there is very little to configure.

magpy.show(s1,s2,sens,
    backend='matplotlib',
    fig_facecolor="pink",
)

image

We could offer fig sensing to Figure and ax sending to Axes

  1. Also not helpful with Pyvista because the Plotter constructor does not accept kwargs - all styles are set via methods/properties. Maybe we can add the styles via the methods ?
magpy.show(s1,s2,sens,
    backend='pyvista',
    fig_background_color="pink",
)

image

should be done like this

pl = pv.Plotter()
# add Magpylib stuff
pl.background_color = "pink"
pl.show()

We really need a set of excellent examples and use-cases. I already outlined them in #650. Lets bring them here in the next comment.

@OrtnerMichael
Copy link
Member

OrtnerMichael commented Aug 18, 2023

List of use-cases - KEEP ADDING

  • change background color
  • change figure title
  • remove axes
  • set camera angle

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.

passt

@OrtnerMichael OrtnerMichael merged commit b9c8be1 into main Aug 23, 2023
12 checks passed
@OrtnerMichael OrtnerMichael deleted the show_and_fig_kwargs branch August 23, 2023 10:01
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.

CustomSource Bug #2 Custom Source Bug #1 showlegend in plotly
2 participants