Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将滑块添加到图形时,动画帧将覆盖_Python_Animation_Matplotlib - Fatal编程技术网

Python 将滑块添加到图形时,动画帧将覆盖

Python 将滑块添加到图形时,动画帧将覆盖,python,animation,matplotlib,Python,Animation,Matplotlib,我有一个3D散点图的动画。动画是使用 FuncAnimation来自matplotlib.animation。一切正常,直到我添加了一个滑块的数字。然后,在动画中的帧持续,我看到每一个新的帧上绘制的旧帧(见附图)。我不太清楚FuncAnimation是如何工作的(反复试验),所以我无法找出突然出现的问题 下面是我如何运行动画的详细说明 动画在我命名为MyAnim的类中处理,初始化该类时,我首先使用以下方法创建三维图形: class MyAnim(object): """Animates

我有一个3D散点图的动画。动画是使用
FuncAnimation
来自
matplotlib.animation
。一切正常,直到我添加了一个滑块的数字。然后,在动画中的帧持续,我看到每一个新的帧上绘制的旧帧(见附图)。我不太清楚
FuncAnimation
是如何工作的(反复试验),所以我无法找出突然出现的问题

下面是我如何运行动画的详细说明


动画在我命名为
MyAnim
的类中处理,初始化该类时,我首先使用以下方法创建三维图形:

class MyAnim(object):
    """Animates points on 3D wireplot"""

    def __init__(self, [...]):

        [...]

        self.fig = plt.figure(figsize = (8,8))            
        self.subplot = self.fig.add_subplot(111, projection='3d')
        self.subplot._axis3don = False  
然后我运行启动动画循环的
MyAnim
方法
anim

def anim(self):
    """Sets the animation loop"""

    frames = [...]
    self.anim = animation.FuncAnimation(self.fig, self.animate3d, frames, init_func = self.init, interval = 50, blit = False, repeat= False)
其中在
self.init
中,我初始化一个虚拟散点图(单个透明(
alpha=0
)点为零)

动画本身涉及重新评估导线图
next_pos
的网格点的位置,以及红色(
x_红,y_红,z_红
)和蓝色(
x_蓝,y_蓝,z_蓝
)的散射点的新位置,然后绘制在导线图顶部

因此,图中的这两个部分是:

def animate3d(self, i):

    plt.cla() 

    [...]

    next_pos = get_next_pos(i)

    [...]

    for j in range(X_SIZE ** 2):            

        step_l = (j) * Z_SIZE
        step_r = (j + 1) * Z_SIZE
        self.subplot_3d_wires = self.subplot.plot_wireframe(next_pos[step_l:step_r, 0], 
                                                            next_pos[step_l:step_r, 1], 
                                                            next_pos[step_l:step_r, 2], 
                                                            rstride = 1, 
                                                            cstride = 1, 
                                                            alpha = 0.2, 
                                                            antialiased = True)

    [...]
    (x_red, y_red, z_red) = get_new_red(i)
    (x_blue, y_blue, z_blue) = get_new_blue(i)

    self.subplot_3d_wires = self.subplot.scatter(x_red, y_red, z_red, s = 150, c = RED,  depthshade = False, lw = 0, alpha = 1)
    self.subplot_3d_wires = self.subplot.scatter(x_blue, y_blue, z_blue, s = 150, c = BLUE,  depthshade = False, lw = 0, alpha = 1)

    [...]

    return [self.subplot_3d_wires]
我想逐渐用滑块条替换动画,因此首先我只想在我的动画旁边添加一个滑块(
(来自matplotlib.widgets import slider
)(不以任何方式连接到动画)。但是仅仅在图上声明silder,动画就被破坏了——我看到我的绘图一个接一个地叠加在一起,见图

我尝试使用

 axcolor = 'lightgoldenrodyellow'
 axtime = plt.axes([0.25, 0.1, 0.65, 0.03])
 self.stime = Slider(axtime, 'Time', 0.0, 100.0, valinit = 50.0)
在类
MyAnim
\uuuuuu init\uuuuu
中,或者在我启动动画的
init
方法时,但它们产生相同的结果

我在这里做错了什么?任何帮助都将不胜感激


问题在于
plt.cla()
。 此命令将清除在绘图上找到的最后一个激活的
轴。添加滑块后,滑块的轴将成为要清除的轴,并且打印的轴保持不变

因此,解决方案不是让pyplot决定清除哪些轴,而是明确地说明

self.subplot.cla()   

即使没有滑块,新的绘图也应该简单地添加到图形中。如果不是这样,那么代码中的某些内容与此处显示的内容不同。我根据您的代码(或我认为您正在做的事情)进行了修补,它还将所有的点添加到画布中,正如预期的那样。你能找出你的真实情况有什么不同吗?@ImportanceOfBeingErnest谢谢,很有趣。。。我真的只是注释掉了滑块,这就不同了。我也使用matplotlib.use(“TkAgg”)
我看不出有什么不同。。。我将试着运行你的代码,看看我得到了什么。不管怎样,你看到解决代码中覆盖问题的方法了吗?@ImportanceOfBeingErnest,你是对的,谢谢!我在
aniamte3d
的开头忽略了
plt.cla()
以删除该图形。编辑的代码是,(我将更新我的帖子)。现在问题出现了,正如我在post-Slider更改外观中所描述的那样。
self.subplot.cla()