#!/usr/bin/env python
# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>. Licence: GPL
# $Id: trajplot.py,v 1.1 2000/10/04 22:30:48 hzhu Exp $
"""
Ploting trajectories
"""
from MatPy.gplot import Gplot,wait
from MatPy.Matrix import Matrix,max2,zeros
from MatPy.efuncs import abs
class TrajPlot(Gplot):
""" Trajectory Plot """
def __init__(g, n=1, size=10, names=[], wait_time=.1):
""" initialize history of system """
Gplot.__init__(g)
g.n = n
g.Xs = []
g.Ys = []
g._bb = (bx, by) = (size, size)
g.names = names
g._wait_time = wait_time
def plotadd(g, ys, xs):
""" add on step to history, determine axes, then plot """
# get past history
Xs = g.Xs
Ys = g.Ys
(bx, by) = g._bb
# Add history
Xs.append(list(xs))
Ys.append(list(ys))
# Calculate plot size
X = Matrix(Xs)
Y = Matrix(Ys)
bX = max2(abs(X))
bY = max2(abs(Y))
change = 0
if bX > bx*.95: bx = bX * 1.15; change = 1
if bY > by*.95: by = bY * 1.15; change = 1
if change: g.axis((-bx, bx), (-by, by))
g._bb = (bx, by)
# plot history
g.plot(Y, X, names=g.names)
g.holdon
g.plot([Matrix(ys)], [Matrix(xs)], with="points 4 6")
g.holdoff
wait(g._wait_time)
#------------------------------------------------------------------
if __name__ == "__main__":
" Plot three random walks "
from MatPy.Stats.distribs import randn
n = 3
g = TrajPlot(names=["red","green","blue"])
#g.axis_equal = 1
xs = zeros(n)
ys = zeros(n)
for i in range (100):
xs += randn(n)*10
ys += randn(n)
g.plotadd(xs, ys)
wait(.5)
|