matplotlib保存bubble动画为gif

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

# 初始化构建所有样本
n = 100
balls = np.zeros(n, dtype=[
    ('position', float, 2),
    ('size', float, 1),
    ('growth', float, 1),
    ('color', float, 4)])
balls['position'] = np.random.uniform(0, 1, (n, 2))
balls['size'] = np.random.uniform(40, 50, n)
balls['growth'] = np.random.uniform(10, 20, n)
balls['color'] = np.random.uniform(0, 1, (n, 4))

# 绘制图像
plt.figure('Bubble', facecolor='lightgray')
plt.title('Bubble', fontsize=16)
sc = plt.scatter(balls['position'][:, 0], balls['position'][:, 1], balls['size'], color=balls['color'])
plt.xticks([])
plt.yticks([])

# 实现动画
def update(number):
    # 更新界面, 让每个点不断变大
    balls['size'] += balls['growth']
    sc.set_sizes(balls['size'])
    index = number % n
    balls['size'][index] = \
        np.random.uniform(40, 70, 1)
    balls['position'][index] = \
        np.random.uniform(0, 1, (1, 2))
    # 更新界面
    sc.set_sizes(balls['size'])
    sc.set_offsets(balls['position'])

# 每隔三十毫秒,执行一次update函数
anim = ma.FuncAnimation(
    plt.gcf(), update, interval=30)
plt.show()
# 使用imagemagick 保存为gif
# 提前 sudo apt-get install imagemagick
anim.save('bubble_animation.gif', writer='imagemagick')

[外链图片转存失败(img-yXtTwZpE-1565424053086)(https://www.exobrain.online/2016/08/12/matplotlib-animation-FuncAnimation/bubble_animation.gif)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值