# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>. Licence: GPL
# $Id: gplot.py,v 1.6 2001/08/26 12:40:43 hzhu Exp $
"""
Wrapper of Gnuplot package for matrices
"""
try:
from Gnuplot import Gnuplot,Data,GridData
except:
print """
Requires the Gnuplot package by Michael Haggerty at
http://monsoon.harvard.edu/~mhagger/Gnuplot/Gnuplot.html
"""
from Numeric import arange
from MatPy.Matrix import to_Matrix,Matrix,to_array,cols
import re
def wait(wtime=None, str=None, prompt='Press any key...\n'):
"""wait(wtime, str, prompt) : wait wtime or
display str , and prompt and wait until input"""
if wtime is None:
if str is not None:
print str
try:
raw_input(prompt)
except EOFError:
print "wait: no interactive input ... continue"
else:
import time
time.sleep(wtime)
class Gplot(Gnuplot):
"""class Gplot(Gnuplot) : gnuplot window as an object with methods for
plot, plotlines, mesh, holdon, holdoff, axis, axes, etc.
The __call__ interface accepts arbitrary gnuplot command as string.
"""
def __init__(g):
"Gplot object, default parametric, lines, grid"
Gnuplot.__init__(g)
g('set parametric')
g('set data style lines')
g('set grid')
g.holdoff
g.axis_equal = 0
g.set_gnuplot_axis(-10,10,-10,10,-10,10)
def __getattr__(self, name):
if name == "holdon":
self.hold = 1
elif name == "holdoff":
self.hold = 0
self.data = []
elif name == "holdoff":
self.hold = 0
self.data = []
else:
try:
return self.__dict__[name]
except KeyError:
raise AttributeError, name
def axis(g, xrange=None, yrange=None, zrange=None,
equal=None):
"""Return existing axis or set new axis, possibly equal x,y
zrange is not carefully implemented at the moment.
Unset axes if no argument given.
"""
if not (xrange or yrange or zrange or equal is not None):
g('set autoscale x; set autoscale y')
return g.get_gnuplot_axis()
# at least one axis is changing
x0, x1, y0, y1, z0, z1 = g.get_gnuplot_axis()
if xrange: x0, x1 = xrange
if yrange: y0, y1 = yrange
if zrange: z0, z1 = zrange
if equal is not None: g.axis_equal = equal
axis_equal = g.axis_equal
if axis_equal:
x1 = y1 = max(x1, y1)
x0 = y0 = min(x0, y0)
g('set xrange [%s:%s]' % (x0, x1))
g('set yrange [%s:%s]' % (y0, y1))
g('set zrange [%s:%s]' % (z0, z1))
def set_gnuplot_axis(g, *xyz):
""" This is not working properly - can't unset, can't get axis"""
x0, x1, y0, y1, z0, z1 = xyz
#g('set xrange [%s:%s]' % (x0, x1))
#g('set yrange [%s:%s]' % (y0, y1))
#g('set zrange [%s:%s]' % (z0, z1))
g._current_axis = x0, x1, y0, y1, z0, z1
def get_gnuplot_axis(g):
"""This does not work because it is a one way pipe"""
#(x0, x1) = g.re_range(g('show xrange'))
#(y0, y1) = g.re_range(g('show yrange'))
#(z0, z1) = g.re_range(g('show zrange'))
x0, x1, y0, y1, z0, z1 = g._current_axis
return x0, x1, y0, y1, z0, z1
re_range = re.compile(r'set .range \[[^:]* : [^]]*\]').match
def set_data(g, x, y, name, with=None):
"set_data(x,y,name,with) : Generate Gnuplot.Data"
#print type(x), type(y), type(name)
if y.shape[1] == 1: y = y.T
y = to_array(y)[0]
if x:
x = to_Matrix(x)
if x.shape[1] == 1: x = x.T
x = to_array(x)[0]
else:
x = arange(len(y))
try:
return Data(x, y, title=name, with=with)
except:
print x.shape, y.shape, type(name)
print x, y
raise
def plot(g, ys, xs=(), names=(), with=None):
"""plot(ys, xs, names, with) : plots multiple lines with x, y, name:
ys can be a list of vectors or a matrix
For staircase plots, set with='steps'
"""
if isinstance(ys, Matrix): ys = cols(ys)
if isinstance(xs, Matrix): xs = cols(xs)
"Generate data: a list of datasets"
#print type(xs), type(ys), type(names)
data = map(g.set_data, xs, ys, names, [with]*len(ys))
if g.hold:
g.data = g.data + data
else:
g.data = data
"Plot on the figure g"
apply(Gnuplot.plot, [g] + g.data)
return g
def plotlines(g, lines, names=(), with=None):
""" Plot lines, each line is a list of 2d points
or a matrix of 2 columns"""
xs = []; ys = []
for line in lines:
if not isinstance(line, Matrix):
line = Matrix(line)
xs.append(line[:,0])
ys.append(line[:,1])
g.plot(ys, xs, names, with)
return g
def mesh(g, z, x, y, no_contour=0, no_hidden=0):
""" Mesh(z,x,y) : mesh z against x and y as axes
Default using contour and hidden"""
z, x, y = to_array(z), to_array(x)[0], to_array(y)[0]
#print z.shape, x.shape, y.shape
d = GridData(z, x, y)
if not no_hidden: g('set hidden')
if not no_contour: g('set contour base')
g.splot(d)
return g
def text(g, point, text):
g('set label "%s" at %s,%s' %(text, point[0], point[1]))
def wait(g, *args):
apply(wait, args)
return g
#------------------------------------------------------------------
if __name__ == "__main__":
g = Gplot()
g.axis((-1,1), (0,0), equal=1)
g.plot([Matrix([3])])
wait()
g.title("This is a test plot")
g.xlabel("the x label")
g.ylabel("the y label")
g.axis((-1,1), (3,10))
g.replot()
print g.axis()
from MatPy.Stats.distribs import randn
#g('set data style linespoints')
g.holdon
for i in range(200):
x = randn((1,2))*2 + 4
g.plotlines([x], with='points %s' %i)
wait(.04)
wait()
|