VPython介绍ppt

Download Report

Transcript VPython介绍ppt

http://www.vpython.org/
VPython
• 同Tkinter一样, 也是一个图形模块
• 支持3D
• 支持对物理系统的模拟
使用VPython:
from visual import *
www.vpython.org
2
visual对象
• 例:box,sphere
• 对象生存于整个程序执行期
• 对象在"显示窗口"中显示
– 一个缺省窗口
– 若有多个窗口, 给窗口命名
• 对象有属性
– 常用的:pos, color, length/height/width, radius等
• 改变对象属性, 即刻改变显示
3
例1
from visual import *
sphere()
Lu Chaojun, SJTU
4
例2
from visual import *
ball = sphere(pos=(-5,0,0), radius=0.5, color=color.cyan)
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green)
5
例3 赋予球物理属性(速度,位置)
ball.velocity = vector(25,0,0)
deltat = 0.005
ball.pos = ball.pos + ball.velocity*deltat
from visual import *
ball = sphere(pos=(-5,0,0), radius=0.5, color=color.cyan)
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green)
ball.velocity = vector(25,0,0)
deltat = 0.005
太快了
rate(100)
t=0
while t < 3:
ball.pos = ball.pos + ball.velocity*deltat
t = t + deltat
6
例 4 物体交互
from visual import *
ball = sphere(pos=(-5,0,0), radius=0.5, color=color.cyan)
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green)
ball.velocity = vector(25,0,0)
deltat = 0.005
t=0
while t < 3:
rate(100)
if ball.pos.x > wallR.pos.x:
ball.velocity.x = -ball.velocity.x
ball.pos = ball.pos + ball.velocity*deltat
t = t + deltat
7
例5 物理规律
from visual import *
floor = box(pos=(0,0,0),size=(4,0.5,4), color=color.blue)
ball = sphere(pos=(0,4,0),radius=1,color=color.red)
ball.velocity = vector(0,-1,0)
dt = 0.01
while True:
rate(100)
ball.pos = ball.pos + ball.velocity * dt
if ball.y < ball.radius:
ball.velocity.y = abs(ball.velocity.y)
else:
ball.velocity.y = ball.velocity.y - 9.8 * dt
Lu Chaojun, SJTU
8
其他VPython对象
• 如: vector
• 不显示
• vector对象支持:
– mag, mag2, norm, cross, dot, rotate等
Lu Chaojun, SJTU
9
VPython用途
• 用可视化方式(如动画)展示数据的变化
• 用可视化方式显示计算结果
from visual import *
def point3D(coord_list):
pt = []
for i in range(len(coord_list)):
pt.append(sphere(pos=coord_list[i],radius=.4,color=(1,1,1)))
for i in range(len(coord_list)):
pt[i].color = (0,1,0) # green
rate(1)
pt[0].color = (1,0,0)
# red
p = [(0,0,0),(1,1,1),(1,1,-1),(2,2,2),(2,2,-2),(3,3,3),(3,3,-3)]
point3D(p)
Lu Chaojun, SJTU
10
VPython作标绘图
• VPython提供强大的作图功能
• 需导入的模块
from visual.graph import *
• 图形
–
–
–
–
线: gcurve
点: gdots
柱: gvbars, ghbars
桶: ghistogram
Lu Chaojun, SJTU
11
例:VPython作图
from numpy import *
from visual.graph import *
f1 = gcurve(color=color.cyan)
for x in arange(0.,8.1,0.1):
f1.plot(pos=(x,5.*cos(2.*x)*exp(-0.2*x)))
Lu Chaojun, SJTU
12
MatPlotLib简介
MatPlotLib
• MPL:二维标绘图(2D plot)库
– 标绘图,直方图,柱状图,散点图等
• 源自MATLAB
– 用Python实现了Matlab
• 其宗旨:任何人都可以通过几条命令就能
绘出简单数据图
• 网上资源:
matplotlib.sourceforge.net/users/
Lu Chaojun, SJTU
14
MatPlotLib的使用
• 需安装numpy库
• 导入模块
from pylab import *
• 将函数表示为坐标(x,y,z)的数组
• 绘制命令:show()
– 通常作为程序的最后一条语句
– 也可交互式使用
• 绘制的图可以存储为许多格式
Lu Chaojun, SJTU
15
例:投资收益
• 以标绘图显示年份和收益
from pylab import *
principal = 10000
apr = 0.05
values = [0] * 10
for i in range(10):
values[i] = principal
principal = principal * (1 + apr)
plot(range(10),values)
show()
Lu Chaojun, SJTU
# 或 bar(range(10),values)等等
16
例:绘制函数图像
from pylab import *
def plot_three():
t = arange(0.0,20.1,0.1)
s = range(21)
plot(s,s,t,2*t,"rs",t,t*t,'go')
xlabel("input size")
ylabel("time")
ti = title("Plotting three functions")
ti.set_color("r")
ti.set_fontsize(14)
ti.set_fontweight("bold")
plot_three()
show()
Lu Chaojun, SJTU
17
End
Lu Chaojun, SJTU
18