zoukankan      html  css  js  c++  java
  • matplotlib之简单动画实现

    import matplotlib.pyplot as mp
    import matplotlib.animation as ma
    import numpy as np
    
    '''
    1.随机生成100个气泡,放入ndarray数组中
    2.每个气泡包含4个属性:color,position,size,growth
    3.绘制这些气泡
    4.编写动画,让气泡不断变大。
    '''
    
    n = 100
    balls = np.zeros(n, dtype=[
        ('position', 'float32', 2),
        ('size', 'float32', 1),
        ('growth', 'float32', 1),
        ('color', 'float32', 4)
    ])
    # 随机生成100个泡泡,初始化
    balls['position'] = np.random.uniform(0, 1, (n, 2))  # uniform平均分布,可以得到n行2列二维数组数组
    balls['size'] = np.random.uniform(40, 70, n)  # uniform平均分布,可以得到n行2列二维数组数组
    balls['growth'] = np.random.uniform(10, 20, n)  # uniform平均分布,可以得到n行2列二维数组数组
    balls['color'] = np.random.uniform(0, 1, (n, 4))  # uniform平均分布,可以得到n行2列二维数组数组
    
    mp.figure('Animation', facecolor='lightgray')
    mp.title('Animation', fontsize=18)
    sc = mp.scatter(balls['position'][:, 0], balls['position'][:, 1], balls['size'], color=balls['color'])
    
    
    # 每隔30ms,更新每个泡泡的大小
    
    def update(number):
        balls['size'] += balls['growth']
        # 每次都选中一个泡泡重新随机属性
        index = number % n
        balls[index]['size'] = np.random.uniform(40, 70, 1)
        balls[index]['position'] = np.random.uniform(0, 1, (1, 2))
        # 重新绘制所有点
        sc.set_sizes(balls['size'])
        sc.set_offsets(balls['position'])
    
    
    anim = ma.FuncAnimation(mp.gcf(), update, interval=1)
    
    mp.show()

       

  • 相关阅读:
    UDP 远程主机强迫关闭了一个现有连接
    CSS float 理解
    C# 启用事务提交多条带参数的SQL语句
    EF学习之DBFirst
    说一说JavaScript 中的原型ProtoType
    Unity
    Unity
    Unity
    Unity
    Godot
  • 原文地址:https://www.cnblogs.com/yuxiangyang/p/11158205.html
Copyright © 2011-2022 走看看