Skip to content

Document how NapariMPLWidget can be used by other plugins #24

Closed
@dstansby

Description

@dstansby

NapariMPLWidget provides Matplotlib canvas and toolbar as a widget. It should be possible for external plugins to use this to avoid them having to create the canvas/toolbar themselves. How they can do this should be documented.

Activity

added
DocsImprovements or additions to documentation
on May 4, 2022
jo-mueller

jo-mueller commented on May 18, 2022

@jo-mueller
Contributor

To pick up this discussion here - how would that work?

One approach for using the canvas within functions for napari would be something like this:

import NapariMPLWidget 

def some_function(arg1: ImageData, arg2: float, napari_viewer: napari.viewer.Viewer = None) -> ImageData:
  # function does something here
  if napari_viewer is not None:
    widget = NapariMPLWidget()
    # plot something on the widget
    napari_viewer.window.add_dock_widget(widget)
  return result

@kevinyamauchi I think the napari_viewer argument is passed automatically if a function is exposed to the napari hook implementation...right?

kevinyamauchi

kevinyamauchi commented on May 20, 2022

@kevinyamauchi
Contributor

Hello @jo-mueller! for the widget spec, the viewer gets passed if one of the widget __init__ kwargs is either napari_viewer or typed as napari.viewer.Viewer. When the user

I think the best pattern for now is if the plugin is a widget plugin and the NapariMPLWidget (or one of its subclasses) is added as a widget to the plugins layout. Something like this:

from napari_matplotlib import ScatterWidget

class MyPlugin(QWidget):
    def __init__(self, napari_viewer: napari.viewer.Viewer):
        # set up some widgets
        my_widget = ...

        # set up the plot widget
        plot_widget =  ScatterWidget(napari_viewer)

        self.layout().addWidget(my_widget)
        self.layout().addWidget(plot_widget)

what do you think? Is this what you had in mind?

dstansby

dstansby commented on May 23, 2022

@dstansby
MemberAuthor

Yes, the above comment from @kevinyamauchi is pretty much what I had in mind. Even though it's possible to use one of the "specialised" widgets, I'm imagining most people will just use NapariMPLWidget to get a blank Matplotlib figure. I'm going to try this out this afternoon with another plugin, so will report back on how it goes!

jo-mueller

jo-mueller commented on Jun 2, 2022

@jo-mueller
Contributor

I was hoping that a plot plugin could somehow be created "on-the-fly" from a function that is exposed to magicgui. But creating a regular pyqt measurement plugin works, too.

self-assigned this
on May 3, 2023
moved this from Todo to In Progress in napari-matplotlib plugin acceleratoron May 5, 2023
added this to the 0.3.0 milestone on May 18, 2023

2 remaining items

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

Metadata

Metadata

Assignees

Labels

DocsImprovements or additions to documentation

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Document how `NapariMPLWidget` can be used by other plugins · Issue #24 · matplotlib/napari-matplotlib