Module for visualizing curves, including animation.
Different classes, CurveVizGnuplot, CurveVizBLT, CurveVizGrace,
implement (approximately) the same interface to various plotting
programs (Gnuplot, BLT, ).
There is a (factory) function, named graph, which offers a unified
interface to all the other plotting classes. The name of the plotting
program is a parameter to graph and the corresponding plotting class
instance is returned.
Example: make a plot using BLT
from py4cs.CurveViz import CurveVizBLT
t = sequence(-4, 4, 0.1, Float)
g = CurveVizBLT(coor=t, parent_frame=some_frame,
ymin=-1.2, ymax=1.2, xlabel=t )
for p in sequence(0, 4, 0.25):
u = exp(-pp)(sin(t) + 0.2sin(5t))
g.plotcurve(u, legend=u(t); p=%g % p)
# programmer can issue any Pmw.Blt.Graph command, e.g.,
g.g.xaxis_configure(logscale=1) # log scale on x axis
All objects have a common interface described below.
def graph(coor=None, program=Gnuplot ,
ymin=None, ymax=None,
xmin=None, xmax=None,
parent_frame=None, sleep=0.5,
xlabel='', ylabel='', title='', window=1):
"""
program: name of plotting program; Gnuplot , BLT , ...
coor : array of coordinates along the x axis
xmin : min coordinate on the x axis
xmax : max coordinate on the x axis
ymin : min coordinate on the y axis
ymax : max coordinate on the y axis
xlabel : label on the x axis
ylabel : label on the y axis
title : headline of the plot
sleep : pause between each window plot (for movies)
window : plot on the screen (True) or not (False)
parent_frame: parent widget if plotting program creates
a part of a larger GUI (only present when using BLT) Example:
from py4cs.CurveViz import graph
t = sequence(-4, 4, 0.1, Float)
g = graph(coor=t, ymin=-1.2, ymax=1.2,
xlabel=t , program=Gnuplot )
for p in sequence(0, 4, 0.25):
u = exp(-pp)(sin(t) + 0.2sin(5t))
g.plotcurve(u, legend=u(t); p=%g % p)
The programmer have direct access to the plotting
program interface through g.g., e.g. (in case of Gnuplot):
g(set nogrid ) # turn default grid off
g.g(set key left box ) # legends in box to the left
def plotcurve(self, y, legend='', ps=0,
plotstyle=DEFAULTSTYLE):
"""
Plot a curve. The variable y is a NumPy array, or
a tuple (x,y) of compatible NumPy arrays for the data
points, legend is the label of the curve, ps!=0
indicates a hardcopy of the plot in PostScript format.
If ps=1 the name of the PostScript file is "tmp_%04d.ps" % f,
where f is a in internal counter. If ps is a string that
string is taken as the filename. The plotstyle argument controls the linetype in the plot.
DEFAULTSTYLE gives the plotting program's default lines.
In general, plotstyle is a dictionary:
'style': lines , dots , points , linepoints
type : 1, 2, 3, ...
size : 1, 2, 3, ...
color : red , black , ...
Note: plotstyle!=DEFAULTSTYLE is not implemented.
"""
raise AttributeError, not implemented in class %s % \
__class__.__name__
def plotcurves(self, curves, ps=0, plotstyles=DEFAULTSTYLE):
"""
Plot a set of curves. The variable curves is
a list/tuple of (array(s),label) tuples, e.g.,
[(y1,data ), (y2,fit )] or [((x1,y1),data ), (y2,fit )],
i.e., the first element in each tuple can either be an
array with the y coordinates or a tuple pair (x,y) of
x and d data points. If ps !=0 a hardcopy of the plot in PostScript format
is made. If ps=1 the name of the PostScript file is
"tmp_%04d.ps" % f, where f is a in internal counter.
If ps is a string that string is taken as the filename.
The plotstyles argument is a list/tuple of
dictionaries describing the style of each curve;
plotstyles[i] describes the data in curves[i][0].
Each plotstyles[i] item follows the syntax of the
plotstyle parameter as documented in plotcurve.
Note: plotstyles!=DEFAULTSTYLE is not implemented.
"""
raise AttributeError, not implemented in class %s % \
__class__.__name__
Imported modules
|
|
import Pmw
from Tkinter import *
from py4cs.numpytools import *
import time
from time import sleep
|
Functions
|
|
graph
plot
test
test2
|
|
graph
|
graph ( program='Gnuplot', **kwargs )
Interface to various classes for curve plotting.
graph is a factory function, which returns an instance
of objects in the CurveViz hierarchy (CurveVizGnuplot,
CurveVizBLT, CurveVizGrace, etc.). Explanations of the arguments:
program: name of plotting program; Gnuplot , BLT , Grace , ...
coor : array of coordinates along the x axis
xmin : min coordinate on the x axis
xmax : max coordinate on the x axis
ymin : min coordinate on the y axis
ymax : max coordinate on the y axis
xlabel : label on the x axis
ylabel : label on the y axis
title : headline of the plot
sleep : pause between each window plot (for movies)
window : plot on the screen (True) or not (False)
parent_frame: parent widget if plotting program creates
a part of a larger GUI (only present when using BLT)
Example:
from py4cs.CurveViz import graph
t = sequence(-4, 4, 0.1, Float)
g = graph(coor=t, ymin=-1.2, ymax=1.2,
xlabel=t , program=Gnuplot )
# make animations:
for p in sequence(0, 4, 0.25):
u = exp(-pp)(sin(t) + 0.2sin(5t))
g.plotcurve(u, legend=u(t); p=%4.2f % p)
# plot several curves in one plot:
t = sequence(0, 10, 0.01)
g.configure(coor=t) # change coordinate vector
g.configure(ymin=-2, ymax=2, ylabel=u )
u1 = sin(t)t; u2 = sin(t)sqrt(t)
g.plotcurves([(u1,t ampl. ),(u2,sqrt(t) ampl. )], ps=1)
The programmer has direct access to the plotting
program interface through g.g., e.g. (in case of Gnuplot):
g(set pointsize 10 )
Exceptions
|
|
ValueError, "program '%s' not supported" % program
|
|
|
plot
|
plot (
x,
y,
legend='',
ps=0,
)
|
|
test
|
test ( program, parent=None )
|
|
test2
|
test2 ( program, parent=None )
|
Classes
|
|
|
|