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..