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画图的基本方法。分享给大家供大家参考,具体如下: Python:使用matplotlib绘制图表 python绘制图表的方法,有个强大的类库matplotlib...

详解opencv Python特征检测及K-最近邻匹配

详解opencv Python特征检测及K-最近邻匹配

鉴于即将启程旅行,先上传篇简单的图像检索介绍,与各位一起学习opencv的同学共勉 一.特征检测 图片的特征主要分为角点,斑点,边,脊向等,都是常用特征检测算法所检测到的图像特征· 1....

用Python解决x的n次方问题

我考虑到了x的所有n次的情况,下面的代码有可能是不完美的,但是肯定是对的。 def aaa(x,n): A=isinstance(x,(int,float)) #这是考虑x和n...

Python实现Logger打印功能的方法详解

前言 众所周知在Python中有专门用于logger打印的套件叫logging,但是该套件logger仅接收一个字符串类型的logger打印信息。因此,我们在使用是需要先提前将要打印的信...

python实现websocket的客户端压力测试

使用python进行websocket的客户端压力测试,这个代码是从github上 找到。然后简单修改了下。大神运用了进程池,以及线程池的内容。所以保存下来,学习学习 然后需要说明的是:...