python连续动画制作_使用matplotlib在python3中对多个形状进行动画处理

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

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()

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

解决方案

You have to use

RB[i].set_xy(temp)

instead of set_XY = temp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值