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实现手写一个类似django的web框架示例

Python实现手写一个类似django的web框架示例

本文实例讲述了Python实现手写一个类似django的web框架。分享给大家供大家参考,具体如下: 用与django相似结构写一个web框架。 启动文件代码: from wsgir...

python3中类的继承以及self和super的区别详解

python中类的继承: 子类继承父类,及子类拥有了父类的 属性 和 方法。 python中类的初始化都是__init__()。所以父类和子类的初始化方式都是__init__(),但是如...

Python调用百度根据经纬度查询地址的示例代码

如下所示: def locatebyLatLng(lat, lng, pois=0): ''' 根据经纬度查询地址 ''' items = {'location': str(...

python的类变量和成员变量用法实例教程

本文实例形式讲解了python的类变量和成员变量用法,对于Python程序设计有一定的参考价值。分享给大家供大家参考。具体如下: 先看看下面这段代码: class TestClass...

python+opencv像素的加减和加权操作的实现

python+opencv像素的加减和加权操作的实现

本文介绍了python+opencv像素的加减和加权操作的实现,分享给大家。 # 目标: # 1、在图像上进行算术操作,如加减以及按位操作 # 2、将会学会使用cv2.add(),...