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

       

  • 相关阅读:
    关于sql json数据的处理
    时间函数strtotime的强大
    /usr/bin/install: cannot create regular file `/usr/local/jpeg6/include/jconfig.h'
    linux安装php7.2.7
    关于sql时间方面的处理
    关于centos防火墙的一些问题
    linux 安装ssl 失败原因
    linux安装php7.2.7
    拾取坐标和反查询接口api
    【转】通过点击获取地址等信息、可以传值
  • 原文地址:https://www.cnblogs.com/yuxiangyang/p/11158205.html
Copyright © 2011-2022 走看看