m1 mac, python assertion failure and crashes when using osx as backend in matplotlib & jupyter notebook

965 Views Asked by At

The error occurs when I was using jupyter notebook

I am using a m1 macbook pro with monterey 12.1, lastest version as this is being written

Here is my code

import matplotlib.pyplot as plt
%matplotlib osx

Here is the error message

2021-12-23 18:16:19.185 python[25121:1046786] *** Assertion failure in +[NSEvent otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:], NSEvent.m:740
2021-12-23 18:16:19.199 python[25121:1046786] *** Assertion failure in +[NSEvent otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:], NSEvent.m:740

This message just repeats hundards of times and shows a recursion error in the end and crashes

Here is the recursion error message

Exception ignored on calling ctypes callback function: <function stop at 0x122abff70>
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniforge/base/envs/strategy/lib/python3.9/site-packages/ipykernel/_eventloop_macos.py", line 107, in stop
    _triggered.set()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/strategy/lib/python3.9/threading.py", line 544, in set
    self._cond.notify_all()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/strategy/lib/python3.9/threading.py", line 381, in notify_all
    self.notify(len(self._waiters))
  File "/opt/homebrew/Caskroom/miniforge/base/envs/strategy/lib/python3.9/threading.py", line 361, in notify
    if not self._is_owned():
  File "/opt/homebrew/Caskroom/miniforge/base/envs/strategy/lib/python3.9/threading.py", line 274, in _is_owned
    if self._lock.acquire(False):
RecursionError: maximum recursion depth exceeded while calling a Python object

While other interactive backends works fine, e.g. nbAgg

1

There are 1 best solutions below

1
soundhead On

I cannot answer your question but add some more info to it, as I went into this in more detail:

When trying to create data plots that are not inlined in a notebook, there is some erroneous behaviour. Either the error described above happens, or the plots are shown and a crash report dialog pops up.

This was tested with the following setup:

macOS Monterey 12.1 (M1 ARM processor)
Python: 3.9.1
matplotlib: 3.5.0
Jupyter: 1.0.0
notebook: 6.4.7
(using a virtual environment)

Example 1:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib osx
plt.plot(np.arange(100))

This leads to the error described in the stack overflow link mentioned above. The cell with the call to plot() seems to be evaluated repeatedly. Sometimes, a plot window will show but get stuck, due to the repeated calls.

Example 2:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('QtAgg')
plt.plot(np.arange(100))

This runs through smoothly, except for the fact that no plot is shown. The last cell’s call returns [<matplotlib.lines.Line2D at 0x12f076bf1>] though. Adding plt.show() does not help either. With TkAgg backend, the same behaviour is observed. Interestingly, when using MacOSX as a backend, I would expect, that the error from Example 1 would occur, but the behaviour is the same as with the other backends…

It remains unclear to me, how plots can be opened externally to the notebook without any errors under the conditions described above.