python实现雨滴下落到地面效果

yipeiwu_com5年前Python基础

本文实例为大家分享了python实现雨滴下落到地面效果的具体代码,供大家参考,具体内容如下

本程序在Windows 64位操作系统下,安装的是Anaconda3-4.2.0

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import animation 
 
# New figure with white background 
fig = plt.figure(figsize=(6,6), facecolor='white') 
 
# New axis over the whole figure, no frame and a 1:1 aspect ratio 
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1) 
 
# Number of ring 
n = 50 
size_min = 50 
size_max = 50 ** 2 
 
# Ring position 
pos = np.random.uniform(0, 1, (n,2)) 
 
# Ring colors 
color = np.ones((n,4)) * (0,0,0,1) 
# Alpha color channel geos from 0(transparent) to 1(opaque) 
color[:,3] = np.linspace(0, 1, n) 
 
# Ring sizes 
size = np.linspace(size_min, size_max, n) 
 
# Scatter plot 
scat = ax.scatter(pos[:,0], pos[:,1], s=size, lw=0.5, edgecolors=color, facecolors='None') 
 
# Ensure limits are [0,1] and remove ticks 
ax.set_xlim(0, 1), ax.set_xticks([]) 
ax.set_ylim(0, 1), ax.set_yticks([]) 
 
def update(frame): 
  global pos, color, size 
 
  # Every ring is made more transparnt 
  color[:, 3] = np.maximum(0, color[:,3]-1.0/n) 
 
  # Each ring is made larger 
  size += (size_max - size_min) / n 
 
  # Reset specific ring 
  i = frame % 50 
  pos[i] = np.random.uniform(0, 1, 2) 
  size[i] = size_min 
  color[i, 3] = 1 
 
  # Update scatter object 
  scat.set_edgecolors(color) 
  scat.set_sizes(size) 
  scat.set_offsets(pos) 
 
  # Return the modified object 
  return scat, 
 
anim = animation.FuncAnimation(fig, update, interval=10, blit=True, frames=200) 
plt.show() 

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python函数的参数常见分类与用法实例详解

本文实例讲述了Python函数的参数常见分类与用法。分享给大家供大家参考,具体如下: 1.形参与实参是什么? 形参(形式参数):指的是 在定义函数时,括号内定义的参数,形参其实就是变量名...

python实现诗歌游戏(类继承)

本文实例为大家分享了python实现诗歌游戏的具体代码,供大家参考,具体内容如下 具体游戏有:根据上句猜下句、猜作者、猜朝代、猜诗名等 如果有更好玩儿的游戏,不妨自己写一下 1.首先,先...

Django认证系统实现的web页面实现代码

结合数据库、ajax、js、Djangoform表单和认证系统的web页面 一:数据模块 扩展了Django中的user表,增加了自定义的字段 from django.db imp...

Python编程之基于概率论的分类方法:朴素贝叶斯

Python编程之基于概率论的分类方法:朴素贝叶斯

概率论啊概率论,差不多忘完了。 基于概率论的分类方法:朴素贝叶斯 1. 概述 贝叶斯分类是一类分类算法的总称,这类算法均以贝叶斯定理为基础,故统称为贝叶斯分类。本章首先介绍贝叶斯分类算法...

Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法

本文档主要描述了Linux下python数据库驱动的安装和配置,用来实现在Linux平台下通过python访问MySQL、Oracle、SQL Server数据库。 其中包括以下几个软件...