优雅地停止Python中的动画图(matplotlib)

1 投票
1 回答
1644 浏览
提问于 2025-04-18 10:19

我正在一个程序中运行一个动画散点图,一切都很正常,除了当我想要退出的时候会出现一个异常。

import multiprocessing as mp
import time
from collections import deque

def start_colored_scores(nb_channels):
    q = mp.Queue()
    process = mp.Process(target=colored_scores,args=(q,nb_channels,4000))
    process.start()
    return process,q

def colored_scores(q,nb_channels,size):
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    fig, axes = plt.subplots(nrows=nb_channels,ncols=1,sharex=True,sharey=True)
    plt.axis([-1.0,1.0,-1.0,1.0])
    scats = [axe.scatter([0], [0], c="white", s=size) for axe in axes]
    def animate(i):
        scores = q.get()
        if scores is None : # this is the external signal saying things should stop
            plt.close()
            return [axe.scatter([0], [0], c="white", s=size) for axe in axes]
        scats = []
        for score,axe in zip(scores,axes):
            score = max(min(1,1-score),0)
            scats.append(axe.scatter([0], [0], c=(1-score,0,score), s=size))
        return scats
    ani = animation.FuncAnimation(fig, animate, interval=1, blit=True)
    plt.show()

比如说,这个是正常工作的:

_,q = start_colored_scores(2)
x = 0
right = 1
time_start = time.time()
while time.time()-time_start < 5:
    if right==1 and x>1.0:
        x = 1.0
        right = -1
    if right==-1 and x<0.0:
        x = 0.0
        right = 1
    x+=right*0.02
    q.put([x,1-x])
    time.sleep(0.02)
q.put(None) # indicating I do not need plotting anymore

print "this is printed ... exception in the process ?"

它的表现和我预期的一样:散点图显示并动画了5秒钟,然后程序继续运行。唯一的问题是出现了一个异常(我猜是在进程中),提示是:

AttributeError: 'NoneType' object has no attribute 'tk'

有没有办法做到完全一样的事情,但避免这个异常呢?或者在某个地方捕捉到这个异常?

1 个回答

1

你可以很简单地捕捉到那个异常:

def colored_scores(q,nb_channels,size):
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    fig, axes = plt.subplots(nrows=nb_channels,ncols=1,sharex=True,sharey=True)
    plt.axis([-1.0,1.0,-1.0,1.0])
    scats = [axe.scatter([0], [0], c="white", s=size) for axe in axes]
    def animate(i):
        scores = q.get()
        if scores is None : # this is the external signal saying things should stop
            plt.close()
            return [axe.scatter([0], [0], c="white", s=size) for axe in axes]
        scats = []
        for score,axe in zip(scores,axes):
            score = max(min(1,1-score),0)
            scats.append(axe.scatter([0], [0], c=(1-score,0,score), s=size))
        return scats
    ani = animation.FuncAnimation(fig, animate, interval=1, blit=True)
    try:
        plt.show()
    except AttributeError: # This will supress the exception
        pass

不过,一旦你捕捉到了这个异常,你会遇到一个新的异常(至少在我的系统上是这样):

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 536, in callit
    func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 141, in _on_timer
    TimerBase._on_timer(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1203, in _on_timer
    ret = func(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 876, in _step
    still_going = Animation._step(self, *args)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 735, in _step
    self._draw_next_frame(framedata, self._blit)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 755, in _draw_next_frame
    self._post_draw(framedata, blit)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 778, in _post_draw
    self._blit_draw(self._drawn_artists, self._blit_cache)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 798, in _blit_draw
    ax.figure.canvas.blit(ax.bbox)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 353, in blit
    tkagg.blit(self._tkphoto, self.renderer._renderer, bbox=bbox, colormode=2)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/tkagg.py", line 20, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError: this isn't a Tk application

我找不到任何方法来抑制这个异常。你可以做的就是直接结束这个子进程,而不是试图给它发送一个关闭的信号:

proc,q = start_colored_scores(2)
x = 0
right = 1
time_start = time.time()
while time.time()-time_start < 5:
    if right==1 and x>1.0:
        x = 1.0
        right = -1
    if right==-1 and x<0.0:
        x = 0.0
        right = 1
    x+=right*0.02
    q.put([x,1-x])
    time.sleep(0.02)
#q.put(None) # indicating I do not need plotting anymore
proc.terminate()

这样做虽然没有通过队列发送信息来得优雅(而且如果你想在子进程中做一些额外的清理,这样做也不允许),但至少不会抛出任何异常。

撰写回答