Ad

Matplotlib Crashes In Plt.show() On MacOS With TkAgg Backend

- 1 answer

The script runs in a for loop, every iteration calls plt.plot() and after looping calls plt.show() - and crashes (screen freezes and need to force restart). No error message is presented before or after crashing. IMO, this is not a memory issue.

Same if I run the script from shell / PyCharm IDE.

INTERESTING POINTS:

  1. I've ran this script many times in the past on the same Mac and same configuration. The only thing that could be relevant is that I've updated my OS few days ago.
  2. When I run the script in a debug mode with stopping point in "plt.show()" - and no crashing and scripts ends without showing the plot.
  3. When running the script, a small window with "python"-ish icon opens. When I press it nothing happens.

I've tried following many other reported issues here, here, and here - with no success.

MacOS version: Mojave 10.14.6

I can't share the code and the data, but to easily reproduce the problem - try running this simple code:

>>> import matplotlib
>>> matplotlib.use("TkAgg")
>>> from matplotlib import pyplot as plt
>>> plt.plot(range(10))
[<matplotlib.lines.Line2D object at 0x1141069e8>]
>>> plt.show()

And the Mac crushes (freezes and need restart).

Packages:

matplotlib 3.0.3 py37h54f8f79_0
numpy 1.16.2 py37hacdab7b_0
numpy-base 1.16.2 py37h6575580_0
pyqt 5.9.2 py37h655552a_2
pyqt5 5.13.0 pypi_0 pypi scikit-learn 0.20.3 py37h27c97d8_0
scipy 1.2.1 py37h1410ff5_0

*conda info*

     active environment : base
    active env location : /Users/nancy/anaconda3
            shell level : 1
       user config file : /Users/nancy/.condarc
 populated config files : /Users/nancy/.condarc
          **conda version : 4.7.11**
    conda-build version : 3.17.8
         **python version : 3.7.3.final.0**
       virtual packages : 
       base environment : /Users/nancy/anaconda3  (writable)
           channel URLs : http://statgen.org/wp-content/uploads/Softwares/pyplink/osx-64
                          http://statgen.org/wp-content/uploads/Softwares/pyplink/noarch
                          https://repo.anaconda.com/pkgs/main/osx-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/osx-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /Users/nancy/anaconda3/pkgs
                          /Users/nancy/.conda/pkgs
       envs directories : /Users/nancy/anaconda3/envs
                          /Users/nancy/.conda/envs
               **platform : osx-64**
             **user-agent : conda/4.7.11 requests/2.21.0 CPython/3.7.3 Darwin/18.7.0 OSX/10.14.6**
                UID:GID : 501:20
             netrc file : None
           offline mode : False
Ad

Answer

According to ImportanceOfBeingErnest comment, Apple doesn't like Tkinter (TkAgg backend) so I needed to change the backend to 'Qt5Agg'

import matplotlib  
matplotlib.use('Qt5Agg')
from matplotlib import pyplot as plt
Ad
source: stackoverflow.com
Ad