|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 绘制动画
import numpy as np
import matplotlib.pyplot as mp
import matplotlib.animation as ma
# 定义100颗球
n = 100
balls = np.zeros(n, dtype=[('position', 'f8', 2),
('size', 'f8', 1),
('color', 'f8', 4),
('growth', 'f8', 1)])
# 对属性进行初始化
balls['position'] = np.random.uniform(0, 1, (n, 2))
balls['size'] = np.random.uniform(80, 100, (n, ))
balls['color'] = np.random.uniform(0, 1, (n, 4))
balls['growth'] = np.random.uniform(10, 20, (n, ))
# for ball in balls:
# print(ball)
# 画图
mp.figure('Ball Animation', facecolor='lightgray')
mp.title('Ball Animation', fontsize=16)
mp.xticks([])
mp.yticks([])
sc = mp.scatter(balls['position'][:, 0],
balls['position'][:, 1],
s=balls['size'], color=balls['color'])
def update(number):
# 更新点的属性
balls['size'] += balls['growth']
# 找到当前爆炸点的下标,重新初始化属性
ind = number % n
balls[ind]['size'] = \
np.random.uniform(80, 100, 1)
balls[ind]['position'] = \
np.random.uniform(0, 1, (1, 2))
sc.set_sizes(balls['size']) # 更新大小
sc.set_offsets(balls['position']) # 更新位置
# 加入动画
a = ma.FuncAnimation(mp.gcf(), update, interval=30)
mp.show()
这个代码运行起来是若干球大小位置不断变化,但是我这个运行没有动画效果是为什么。
|
|