0

i am using Python 2.7 (Anaconda) with matplotlib to get the animation of a circle patch on a desk (square patch); i'm triyng for my first time to use classes for the patches to get the animation of the circle so that it starts from a defined initial point and then i generate random positions in which the circle should appear flickering (ini_position, then random_position, random, and random again..). But when i try to remove the patch -so that i can use the func animate i have to pass to FuncAnimation- to get a flickering circle in random positions instead of get the multiple plot of the circle in all the random positions, i get the error "NotImplementedError: cannot remove artist". Here is my code:

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)

Any suggestion about how to fix it? I've also tried with self.figure.set_visible(False)but with no luck..

mefit0
  • 11
  • 4
  • If the previous answer solved that initial question why didnt you accept it? – ImportanceOfBeingErnest Jun 28 '17 at 13:37
  • The previous answer uses a FuncAnimation, why don't you use it here? – ImportanceOfBeingErnest Jun 28 '17 at 13:49
  • @ImportanceOfBeingErnest i understood the way funcAnimation works and how to set the parameters but i don't get how to use it with classes... in the funcAnimation i use fig=the figure in which i need the animation to be plot; init_func=i create an object to call the class of the "desk" and use it for the initialization; for the frames i can create a list/tuple with all the random positions of the circle; but i'm not able to call the "animate" that should be a method from circle class.. – mefit0 Jun 29 '17 at 14:59
  • There is no `animate` function in the code. You need to create one and provide it to `FuncAnimation`. – ImportanceOfBeingErnest Jun 30 '17 at 12:26
  • @ImportanceOfBeingErnest i've created animate function but it seems the problem is not in the FuncAnimation but in the method i use to clear the figure.. thank you for the tips, i really appreciate – mefit0 Jul 01 '17 at 16:50
  • This is just big chaos. You're creating several figures and several axes (I'm not sure in which figure the axes are created) You're also trying to remove artists which are different from the one you added previously. Maybe you take a step back, take pen and pencil and create a clear workflow for your program first. – ImportanceOfBeingErnest Jul 01 '17 at 17:12

0 Answers0