Matplotlib - Tcl_AsyncDelete: async handler deleted by the wrong thread? Matplotlib - Tcl_AsyncDelete: async handler deleted by the wrong thread? django django

Matplotlib - Tcl_AsyncDelete: async handler deleted by the wrong thread?


By default matplotlib uses TK gui toolkit, when you're rendering an image without using the toolkit (i.e. into a file or a string), matplotlib still instantiates a window that doesn't get displayed, causing all kinds of problems. In order to avoid that, you should use an Agg backend. It can be activated like so --

import matplotlibmatplotlib.use('Agg')from matplotlib import pyplot

For more information please refer to matplotlib documentation -- http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server


The above (accepted) answer is a solution in a terminal environment. If you debug in an IDE, you still might wanna use 'TkAgg' for displaying data. In order to prevent this issue, apply these two simple rules:

  1. everytime you display your data, initiate a new fig = plt.figure()
  2. don't close old figures manually (e.g. when using a debug mode)

Example code:

import matplotlibmatplotlib.use('TkAgg')from matplotlib import pyplot as pltfig = plt.figure()plt.plot(data[:,:,:3])plt.show()

This proves to be the a good intermediate solution under MacOS and PyCharm IDE.


If you don't need to show plots while debugging, the following works:

import matplotlibmatplotlib.use('Agg')from matplotlib import pyplot as plt

However, if you would like to plot while debugging, you need to do 3 steps:

1.Keep backend to 'TKAgg' as follows:

import matplotlibmatplotlib.use('TKAgg')from matplot.lib import pyplot as plt

or simply

import matplotlib.pyplot as plt

2.As Fábio also mentioned, you need to add fig(no. #i)=plt.figure(no.#i) for each figure #i. As the following example for plot no.#1, add:

fig1 = plt.figure(1)plt.plot(yourX,yourY)plt.show()

3.Add breakpoints. You need to add two breakpoints at least, one somewhere at the beginning of your codes (before the first plot), and the other breakpoint at a point where you would like all plots (before to the second breakpoint) are plotted. All figures are plotted and you even don't need to close any figure manually.