使用matplotlib在python3中对多个形状进行动画处理 [英] Animate multiple shapes in python3 using matplotlib

查看:429
本文介绍了使用matplotlib在python3中对多个形状进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用matplotlib动画功能同时在python3中为多个对象制作动画。

Trying to animate multiple objects at once in python3 while using matplotlib animation function.

下面是我编写的代码。我能够创建多个对象并在图中显示它们。我通过使用一个for循环来完成此任务,该循环包含一个矩形的补丁函数。从这里开始,我希望通过使用动画功能将所有单个矩形移动一定数量。

code written below is where I am thus far. I am able to create the multiple objects and display them in the figure. I did this by using a for loop containing a patches function for a rectangle. From here I was hoping to move all the individual rectangles over by a set amount by using the animation function.

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

fig = plt.figure()
ax = fig.add_subplot(111)

plt.xlim(-100, 100)
plt.ylim(-100, 100)

width = 5
bars = 25

RB = [] # Establish RB as a Python list
for a in range(bars):
    RB.append(patches.Rectangle((a*15-140,-100), width, 200, 
          color="blue", alpha=0.50))

def init():
    for a in range(bars):
        ax.add_patch(RB[a])
    return RB

def animate(i):
    for a in range(bars):
        temp = np.array(RB[i].get_xy())   
        temp[0] = temp[0] + 3;
        RB[i].set_XY = temp
    return RB

anim = animation.FuncAnimation(fig, animate, 
                           init_func=init, 
                           frames=15, 
                           interval=20,
                           blit=True)

plt.show()

当前,一旦运行代码,什么都不会发生。我尝试遵循python网站上的示例;但通常会导致'AttributeError:'list'对象没有属性'set_animated'。

Currently, nothing moves or happens once I run the code. I have tried to follow the examples found on the python website; but it usually results in a 'AttributeError: 'list' object has no attribute 'set_animated''.

推荐答案

 RB[i].set_xy(temp)

而不是 set_XY = temp

这篇关于使用matplotlib在python3中对多个形状进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆