NotImplementedError:无法删除艺术家,调用函数一次删除一个补丁(matplotlib)

时间:2017-06-28 13:24:28

标签: python class animation matplotlib figures

我正在使用带有matplotlib的Python 2.7(Anaconda)来获取桌面上的圆形补丁动画(方形补丁);我第一次尝试使用修补程序的类来获取圆的动画,使其从定义的初始点开始,然后生成随机位置,其中圆圈应该出现闪烁(ini_position,然后是random_position ,随机,再次随机..)。但是当我尝试删除补丁时 - 我可以使用func animate我必须传递给FuncAnimation - 在随机位置获得一个闪烁的圆圈而不是获得圆圈的多个图所有随机位置,我得到错误" NotImplementedError:无法删除艺术家"。这是我的代码:

import matplotlib.pyplot as plt
import matplotlib.patches as ptc
from matplotlib import animation
import numpy as np

###############################################################################
##########################_____POLYGON_____####################################
class circle(object):    
    def __init__(self,xy,radius,color):
        self.xy=xy
        self.radius=radius
        self.color=color 

    def randPos(self,numTri,frames):
        self.numTri=numTri
        self.frames=[frames]
        for i in range (0,numTri):
            frames.append(np.random.uniform(1,9,2))

    def plotDef(self): 
        self.figure=ptc.Circle((self.xy),self.radius, 
                            facecolor = self.color, edgecolor="none")

    def addPlt(self):
        self.fig=plt.figure(1) 
        self.ax1 = self.fig.add_subplot(111,aspect='equal')       
        self.ax1.set_xlim((0,10))
        self.ax1.set_ylim((0,10))
        self.ax1.add_patch(self.figure)     

    def removePlt(self):
        self.plotDef()
        self.figure.remove()

class regularPoly(circle):
    def __init__(self,xy,vertices,radius,orient,fill,color,edge):
        super(regularPoly, self).__init__(xy,radius,color)
        self.vertices = vertices
        self.orient = orient
        self.fill = fill
        self.edge = edge

    def plotDef(self): 
        self.figure = ptc.RegularPolygon((self.xy),self.vertices,
                           self.radius, orientation=self.orient, fill=self.fill,
                           facecolor = self.color, edgecolor=self.edge)
    def addPlt(self):
        super(regularPoly,self).addPlt()

    def removePlt(self):
        self.figure.set_visible(False)


    def clearPlt(self):
        self.ax1.clear()

###############################################################################
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%___FIGURES_INITIALIZATION
#DESK            
deskCoord=np.array([5.0,5.0],dtype=float)               
deskNumVer=int(4)
deskRad=np.array([6.0],dtype=float)
deskOrie=np.array([0.785],dtype=float)
deskFill=False
deskCol="none"
deskEdge="black"

#HAND
ini_handCoord=np.array([5.0,1],dtype=float)
handRad=np.array([0.2],dtype=float)
handCol="y"
handPosList=[]

#ANIMATION
fig=plt.figure(1)
frames=[]
numTri=5
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%____________________________ 
def initDesk():
    objDesk=regularPoly(deskCoord,deskNumVer,deskRad,deskOrie,deskFill, 
                deskCol,deskEdge)

    objHand=circle(ini_handCoord,handRad,handCol)
    objHand.randPos(numTri,handPosList)

    objDesk.plotDef()
    objDesk.addPlt()

    objHand.plotDef()
    objHand.addPlt()

def animate(position):
    objHand=circle(position,handRad,handCol)
    objHand.plotDef()
    objHand.addPlt()
    objHand.removePlt()

anim = animation.FuncAnimation(fig, animate, frames=handPosList, init_func=initDesk,
                               repeat=False, interval=1000)

有关如何修复它的任何建议?我也试过self.figure.set_visible(False),但没有运气..

0 个答案:

没有答案