matplotlib / ipympl

Matplotlib Jupyter Integration
https://matplotlib.org/ipympl/
BSD 3-Clause "New" or "Revised" License
1.57k stars 227 forks source link

PolygonSelector does not work in JupyterLab #476

Open nvaytet opened 2 years ago

nvaytet commented 2 years ago

Describe the issue

The PolygonSelector example from the mpl docs works very well in the default Qt backend. Screenshot at 2022-07-05 17-01-56

However, using ipympl in a notebook, nothing happens. No gray point follows the mouse cursor, nothing is drawn when I click. Just the initial figure with the blue dots. I checked the jupyterlab log console on debug level but there are no errors.

I was wondering if it was related to https://github.com/matplotlib/matplotlib/issues/15852, and tried adding useblit=False where the PolygonSelector is created, but this did not help.

Versions

ianhi commented 2 years ago

I think the key is in the blocking (or non-blocking) behavior of plt.show()

At the end of the example there are these lines:

plt.show()

selector.disconnect()

When run from a python file the show is blocking, however in a notebook the show doesn't block execution so the disconnect is run immediately. When I comment out selector.disconnect() then it works for me.


That was surprisingly difficult to notice. My strategy for figuring it out was to:

  1. Check if it worked with either %matplotlib nbagg or %matplotlib qt - neither worked
  2. Try just using a bare PolygonSelector (which worked
  3. figure out the difference between a polygonselector and selectfromcolleciton
  4. accidently delete the disconnect line and things start working
  5. figure out that I deleted that line and then understood

parts of that strategy are generally applicable to all ipympl bugs. notably trying them in other backends launched form the notebook.

nvaytet commented 2 years ago

Ok thanks. It seems to work. However, the behaviour of the Shift key is weird, or definitely not like in the Qt window. At first it just seems to do nothing, but then if I release the mouse button, or shift (or both I am not sure anymore), suddenly the polygon moves with the mouse whenever I hold shit (no need to click with the mouse again). The whole thing is quite unusable.

But at least I can see the polygon now (and move the vertices, and everything else). Thanks!