Python NotImplementedError:无法删除艺术家,调用函数一次删除一个补丁(matplotlib)
我使用Python2.7(Anaconda)和matplotlib来获得桌面上圆形补丁(方形补丁)的动画;我是triyng,这是我第一次使用面片类来获得圆的动画,这样它从定义的初始点开始,然后我生成随机位置,在其中圆应该闪烁(ini_位置,然后随机_位置,随机,再次随机..)。但是,当我尝试删除面片时,为了使用funcPython NotImplementedError:无法删除艺术家,调用函数一次删除一个补丁(matplotlib),python,class,animation,matplotlib,figures,Python,Class,Animation,Matplotlib,Figures,我使用Python2.7(Anaconda)和matplotlib来获得桌面上圆形补丁(方形补丁)的动画;我是triyng,这是我第一次使用面片类来获得圆的动画,这样它从定义的初始点开始,然后我生成随机位置,在其中圆应该闪烁(ini_位置,然后随机_位置,随机,再次随机..)。但是,当我尝试删除面片时,为了使用funcanimate我必须传递到FuncAnimation,以在随机位置获得闪烁的圆,而不是在所有随机位置获得圆的多个绘图,我会得到错误“NotImplementedError:cann
animate
我必须传递到FuncAnimation
,以在随机位置获得闪烁的圆,而不是在所有随机位置获得圆的多个绘图,我会得到错误“NotImplementedError:cannot remove Artister”。这是我的密码:
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)
,但运气不佳。如果前面的答案解决了最初的问题,为什么你不接受它?前面的答案使用了一个函数,为什么不在这里使用它?@ImportanceOfBeingErnest我了解funcAnimation的工作方式以及如何设置参数,但我不知道如何在类中使用它。。。在funcAnimation中,我使用fig=需要绘制动画的图形;init_func=i创建一个对象来调用“desk”的类并将其用于初始化;对于帧,我可以创建一个列表/元组,其中包含圆的所有随机位置;但是我不能调用应该是circle类中的方法的“animate”。代码中没有animate
函数。您需要创建一个并将其提供给FuncAnimation
@ImportanceOfBeingErnest我创建了animate函数,但问题似乎不在FuncAnimation中,而是在我用于清除图形的方法中。。谢谢你的提示,我真的很感激