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

yipeiwu_com6年前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设计】。

相关文章

Django原生sql也能使用Paginator分页的示例代码

django-pagination这是一个python包,来自github上的一个项目,很容易用。 不过这是一个懒人工具,好吧(工具理性)。不过当一个页面有多处需要采用分页的话,就行不...

Django中使用haystack+whoosh实现搜索功能

Django中使用haystack+whoosh实现搜索功能

为了实现项目中的搜索功能,我们使用的是全文检索框架haystack+搜索引擎whoosh+中文分词包jieba 安装和配置 安装所需包 pip install django-hays...

python获取微信企业号打卡数据并生成windows计划任务

python获取微信企业号打卡数据并生成windows计划任务

由于公司的系统用的是Java版本,开通了企业号打卡之后又没有预算让供应商做数据对接,所以只能自己捣鼓这个,以下是个人设置的一些内容,仅供大家参考 安装python python的安装,这...

在Python程序中操作文件之isatty()方法的使用教程

 如果文件已连接(与终端设备相关联)到一个tty(状)的设备,isatty()方法返回True,否则返回False。 语法 以下是isatty()方法的语法: fileOb...

详解python播放音频的三种方法

第一种 使用pygame模块 pygame.mixer.init() pygame.mixer.music.load(self.wav_file) pygame.mix...