anntzer / mplcursors

Interactive data selection cursors for Matplotlib.
https://mplcursors.readthedocs.io
zlib License
113 stars 19 forks source link

`Attribute Error` combining matplotlib 3.7.1 and mplcursors on data selection #60

Closed MrAnimaniac closed 1 year ago

MrAnimaniac commented 1 year ago

Bug summary

If you combine mplcursors (0.5.1 or 0.5.2) and matplotlib 3.7.1, you'll get an AttributeError: 'NoneType' object has no attribute 'canvas' after clicking a few data points. Henceforth, selecting a new data point will trigger the same traceback. Otherwise, it works fine.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
import mplcursors as mpl

x = np.arange(1, 11)    
y1 = x

plt.scatter(x,y1)

mpl.cursor()
plt.show()

Actual outcome

Traceback (most recent call last):
  File "C:\Users\MrAni\Python\miniconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 304, in process
    func(*args, **kwargs)
  File "C:\Users\MrAni\Python\miniconda3\lib\site-packages\matplotlib\offsetbox.py", line 1550, in on_release
    if self._check_still_parented() and self.got_artist:
  File "C:\Users\MrAni\Python\miniconda3\lib\site-packages\matplotlib\offsetbox.py", line 1560, in _check_still_parented
    self.disconnect()
  File "C:\Users\MrAni\Python\miniconda3\lib\site-packages\matplotlib\offsetbox.py", line 1568, in disconnect
    self.canvas.mpl_disconnect(cid)
  File "C:\Users\MrAni\Python\miniconda3\lib\site-packages\matplotlib\offsetbox.py", line 1517, in <lambda>
    canvas = property(lambda self: self.ref_artist.figure.canvas)
AttributeError: 'NoneType' object has no attribute 'canvas'

Expected outcome

No terminal output

Additional information

Using matplotlib 3.7.0 or lower works fine. Using a conda install or pip install doesn't affect the output.

I originally posted this as a matplotlib bug but was asked to report it here instead: https://github.com/matplotlib/matplotlib/issues/25440

anntzer commented 1 year ago

Actually the problem needs to be fixed on Matplotlib's side; I opened a PR to fix that (https://github.com/matplotlib/matplotlib/pull/25442). Thanks for the report!