Why matplotlib requires to plot only in the main thread?

前端 未结 2 1865
闹比i
闹比i 2021-01-20 15:58

I\'m trying to plot live the output of a generator.

The following code works as expected (Ctrl-C terminates execution):

import numpy as np
import pyl         


        
2条回答
  •  天涯浪人
    2021-01-20 16:25

    It's probably the GUI that you're using for backend. The GUI likely expects to find itself in the main thread, but it isn't when matplotlib calls get_current_fig_manager().canvas.draw().

    For example, when I do this, I get the following traceback:

    Exception in thread Thread-1:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
        self.run()
      File "/usr/lib/python2.7/threading.py", line 763, in run
        self.__target(*self.__args, **self.__kwargs)
      File "tmp.py", line 18, in plotter
        p.draw()
      File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 555, in draw
        get_current_fig_manager().canvas.draw()
      File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 349, in draw
        tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
      File "/usr/lib/pymodules/python2.7/matplotlib/backends/tkagg.py", line 13, in blit
        tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
    RuntimeError: main thread is not in main loop
    

    Note the tk.call(...) line. The exception you get is not raised from matplotlib, it's raised from TkInter.

提交回复
热议问题