~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/xplt/gistdemohigh.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 1996, 1997, The Regents of the University of California.
 
2
# All rights reserved.  See Legal.htm for full text and disclaimer.
 
3
 
 
4
from Numeric import *
 
5
from scipy_base.fastumath import *
 
6
from GistPlotter import *
 
7
GraphicsError = "GraphicsError"
 
8
import os
 
9
try:
 
10
   graphics = os.environ["PYGRAPH"]
 
11
except KeyError:
 
12
   graphics = "Gist"
 
13
 
 
14
if graphics [0:3] == "Nar" :
 
15
   import NarPlotter
 
16
elif graphics == "Gist" :
 
17
   import GistPlotter
 
18
else :
 
19
   raise GraphicsError , \
 
20
      graphics + " is an unknown graphics package. Check PYGRAPH " + \
 
21
         "environment variable."
 
22
 
 
23
def span (lb, ub, n) :
 
24
   if n < 2: raise ValueError, "Third argument must be at least 2."
 
25
   b = lb
 
26
   a = (ub - lb) / (n - 1.0)
 
27
   return map(lambda x, A=a, B=b: A*x+B,range(n))
 
28
 
 
29
def a3 (lb, ub, n) :
 
30
   return reshape (array(n*span(lb,ub,n), Float), (n,n))
 
31
 
 
32
def mag ( *args ) :
 
33
   r = 0
 
34
   for i in range (len (args)) :
 
35
      r = r + args[i] * args[i]
 
36
   return sqrt (r)
 
37
 
 
38
def a2 (lb, ub, n) :
 
39
   return reshape (array ((n - 1) * spanz (lb, ub, n),  Float), (n-1, n-1))
 
40
 
 
41
def spanz (lb, ub, n) :
 
42
   if n < 3 : raise ValueError, "3rd argument must be at least 3"
 
43
   c = 0.5 * (ub - lb) / (n - 1.0)
 
44
   b = lb + c
 
45
   a = (ub -c -b) / (n - 2.0)
 
46
   return map (lambda x, A = a, B = b: A * x + B, range (n - 1))
 
47
 
 
48
def init ( self ) :
 
49
    self.t = 2*pi*arange (400, typecode = Float) / 399.0
 
50
    self.na1 = 1
 
51
    self.nb1 = 5
 
52
    self.na2 = 2
 
53
    self.nb2 = 7
 
54
    self.rc1 = 40.
 
55
    self.rc2 = 160.
 
56
    self.size = 40.
 
57
    self.phase = self.theta = 0.
 
58
    self.dtheta = pi / (self.nsteps - 1)
 
59
    self.dphase = 2 * pi / (self.nsteps - 1)
 
60
 
 
61
def calc1 ( self ) :
 
62
    self.cost = cos (self.theta)
 
63
    self.sint = sin (self.theta)
 
64
    self.x = self.rc1 * self.cost + self.size * cos (self.na1 * self.t)
 
65
    self.y = self.rc1 * self.sint + self.size * sin (self.nb1 * self.t +
 
66
       self.phase)
 
67
 
 
68
def calc2 ( self ) :
 
69
    self.x = self.rc2 * self.cost + self.size * cos (self.na2 * self.t)
 
70
    self.y = self.rc2 * self.sint + self.size * sin (self.nb2 * self.t +
 
71
       self.phase)
 
72
 
 
73
def incr  ( self ) :
 
74
    self.theta = self.theta + self.dtheta
 
75
    self.phase = self.phase + self.dphase
 
76
 
 
77
from curve import Curve
 
78
from graph2d import *
 
79
def paws ( ) :
 
80
    i = raw_input ("Type in any string to continue; ^C to return to prompt. ")
 
81
    return
 
82
 
 
83
print "\nComprehensive graphics test (sort of) for Python interface"
 
84
print "with Narcisse and (mainly) Gist. This mirrors Lee's low level"
 
85
print "routine. Each frame will be described at the terminal; compare"
 
86
print "what you see with the description, then hit <RETURN> to see"
 
87
print "the next test, or ^C (ctrl-C) to escape.\n"
 
88
print "Type 'demo()' to begin.\n"
 
89
 
 
90
from quadmesh import *
 
91
from cellarray import *
 
92
from lines import *
 
93
from animation2d import *
 
94
 
 
95
 
 
96
def demo ( ) :
 
97
   c1 = Curve ( y = [0,1] , marks = 1 , marker = "A")
 
98
   pl = Plotter ( dpi = 75 )
 
99
   g1 = Graph2d ( c1, plotter = pl , titles = "Curve marked with A" ,
 
100
               title_colors = "blue")
 
101
   g1.plot ( )
 
102
   print "\nA small (75 dpi) window with line marked A from (0,0) to (1,1)."
 
103
   paws ( )
 
104
#  pl = Plotter ( dpi = 100 )
 
105
#  g1 = Graph2d ( c1, plotter = pl , titles = "Curve marked with A" ,
 
106
#              title_colors = "fg")
 
107
#  g1.plot ( )
 
108
#  print "\nA large (100 dpi) window with line marked A from (0,0) to (1,1)."
 
109
#  paws ( )
 
110
   c2 = Curve ( y = [1,0] , marks = 1 , marker = "B")
 
111
   g1.add (c2)
 
112
   g1.change (titles = "New curve marked B.")
 
113
   g1.plot ( )
 
114
   print "\nAdded line marked B from (0,1) to (1,0) in previous plot."
 
115
   paws ( )
 
116
   c1.set ( x = [1,2] )
 
117
   c2.set ( x = [1,2] )
 
118
   g1.change (axis_scales = "loglin", titles = "Same, x axis now logarithmic.")
 
119
   g1.plot ( )
 
120
   print "\nSame, x axis now logarithmic."
 
121
   paws ( )
 
122
   g1.change (x_axis_scale = "lin", titles = "x axis back to linear.")
 
123
   g1.plot ( )
 
124
   print "\nSame, x axis now linear again."
 
125
   paws ( )
 
126
   g1.change(axis_limits=[[1.2,1.8],[0.2,0.8]],
 
127
          titles="Limits now 1.2<x<1.8, 0.2<y<0.8.")
 
128
   g1.plot ( )
 
129
   print "\nLimits now 1.2<x<1.8, 0.2<y<0.8."
 
130
   paws ( )
 
131
#  g1.change(y_axis_limits=[0.4,0.6],
 
132
#         titles="Limits now 1.2<x<1.8, 0.4<y<0.6")
 
133
#  g1.plot ( )
 
134
#  print "\nLimits now 1.2<x<1.8, 0.4<y<0.6"
 
135
#  paws ( )
 
136
   g1.change(axis_limits="defaults",
 
137
          titles="Limits now back to extreme values.")
 
138
   g1.plot ( )
 
139
   print "\nLimits now back to extreme values."
 
140
   paws ( )
 
141
   x=10*pi*arange(200, typecode = Float)/199.0
 
142
   c1 = Curve ( x = x , y = sin(x),marks = 1, marker= "A")
 
143
   g1.delete (2)
 
144
   g1.delete (1)
 
145
   g1.add (c1)
 
146
   g1.change (titles = "Five cycles of a sine wave, marked A.")
 
147
   g1.plot ( )
 
148
   print "\nFive cycles of a sine wave."
 
149
   paws ( )
 
150
   c1.set(marks = 0, width = 6 , type = "dash")
 
151
   g1.change (titles = "A mark gone, bold dashed.")
 
152
   g1.plot ( )
 
153
   print "\nTurn off A marker, plot curve as bold dashed."
 
154
   paws ( )
 
155
   x=2*pi*arange(200, typecode = Float)/199.0
 
156
   crvs = []
 
157
   for i in range (1,7) :
 
158
      r = 0.5*i -(5-0.5*i)*cos(x)
 
159
      s = `i`
 
160
      crvs.append(Curve(y=r*sin(x),x=r*cos(x),marks=0, color=-4-i,label=s))
 
161
   g1=Graph2d(crvs,plotter = pl,titles="Nested cardioids in colors")
 
162
   g1.plot ( )
 
163
   print "\nNested cardioids in primary and secondary colors."
 
164
   paws()
 
165
   g1.change(titles = ["colors","cardioids","Nested","in many"],
 
166
          title_colors = ["red","yellow","magenta","blue"])
 
167
   g1.plot ( )
 
168
   print "\nThe same with four titles in many colors."
 
169
   paws()
 
170
   mks = ['.','+','*','o','x','A']
 
171
   for i in range (1,7) :
 
172
      crvs[i-1].set (color="fg", type = 0, marker = mks[i-1])
 
173
   g1.change (titles = "Marked curves", title_colors = "fg")
 
174
   g1.plot ( )
 
175
   print "\nChanges color to foreground, types to no lines."
 
176
   print "Markers are point, plus, asterisk, circle, cross, and A."
 
177
   paws()
 
178
   for i in range (1,6) :
 
179
      crvs[i-1].set (marks = 0, type = i-1)
 
180
   crvs[i].set (type = 1, width = 4)
 
181
   g1.change (titles = "Different curve types.")
 
182
   print "\nChanges line types to solid, dash, dot, dashdot, dashdotdot."
 
183
   print "Outermost cardioid becomes a thick solid curve."
 
184
   g1.plot()
 
185
   paws ()
 
186
   ValueError = "ValueError"
 
187
   x=a3(-1,1,26)
 
188
   y=transpose (x)
 
189
   zz=x+1j*y
 
190
   zz=5.*zz/(5.+zz*zz)
 
191
   xx=zz.real
 
192
   yy=zz.imaginary
 
193
   q1 = QuadMesh ( x=array(xx, copy = 1), y=array(yy, copy = 1) )
 
194
   g1.new(q1, plotter = pl, titles = "Round mesh with bites out of sides",
 
195
      axis_limits = [[min(ravel(xx)),max(ravel(xx))],
 
196
                     [min(ravel(yy)),max(ravel(yy))]])
 
197
   g1.plot()
 
198
   print "\nQuadrilateral mesh -- round with bites out of its sides."
 
199
   paws (  )
 
200
   q1.set (boundary = 1, vx = x+.5, vy=y-.5,ktype = "none", ltype = "none")
 
201
   g1.change(titles = "Vector field on mesh.")
 
202
   g1.plot ( )
 
203
   print "\nVelocity vectors. Try zooming and panning with mouse."
 
204
   paws ( )
 
205
   q1.set (vx = None, vy = None, z = mag (x+.5,y-.5),marks = 1,
 
206
      boundary=1,boundary_type="dash",marker="A")
 
207
   g1.change(titles = "Lettered contour plot, dashed boundary.")
 
208
   g1.plot ( )
 
209
   print "\nLettered contours, with boundary dashed."
 
210
   paws ( )
 
211
   q1.set (filled=1, type = "dash", color = "white")
 
212
   g1.plot ( )
 
213
   print "\nFilled lettered dashed contours, with boundary dashed."
 
214
   paws ()
 
215
   q1.set(marks = 0,boundary_type="solid",levels=array([0.5,1.0,1.5]),
 
216
       filled=1, contours = 1, type = "dash", color = "white",
 
217
       width = 3)
 
218
   g1.change(titles = "Filled mesh, dashed contours")
 
219
   g1.plot ( )
 
220
   print "\nFilled mesh (color only) with three dashed contours overlaid."
 
221
   paws()
 
222
   print "\nAfter each new palette is installed, hit <RETURN> for the next."
 
223
   pal=["heat.gp","stern.gp","rainbow.gp","gray.gp","yarg.gp","earth.gp"]
 
224
   for i in range(6):
 
225
      g1.quick_plot(color_card = pal[i],titles = "Palette " + pal[i])
 
226
   #  g1.plot ( )
 
227
      paws ( )
 
228
   pal = ["vg.gs", "boxed.gs", "vgbox.gs", "nobox.gs", "work.gs"]
 
229
   q1.set (marks = 1,boundary_type="dash",levels = 8, filled = 0, color = "fg")
 
230
   for i in range (5) :
 
231
      pl.set_style (pal [i])
 
232
##    g1 = Graph2d (q1, plotter = pl, titles = "Style Name: " + pal[i])
 
233
      g1.change (plotter = pl, titles = "Style Name: " + pal[i])
 
234
      g1.plot ( )
 
235
      paws ( )
 
236
   x=a3(-6,6,200)
 
237
   y=transpose (x)
 
238
   r=mag(y,x)
 
239
   theta=arctan2(y,x)
 
240
   funky=cos(r)**2*cos(3*theta)
 
241
   c1 = CellArray(z=funky)
 
242
   g1.replace (1,c1)
 
243
   g1.change (color_card = "earth.gp",
 
244
           titles ="Cell array, three cycles in theta,r",
 
245
           axis_limits = "defaults")
 
246
   g1.plot()
 
247
   paws()
 
248
 
 
249
   theta = a2 (0, 2*pi, 18)
 
250
   x = cos (theta)
 
251
   y = sin (theta)
 
252
 
 
253
   ln = Lines (x0 = x, y0 = y, x1 = transpose (x), y1 = transpose (y))
 
254
   g1 = Graph2d (ln, plotter = pl, xyequal = 1, titles = "Seventeen pointed star.")
 
255
   g1.plot ()
 
256
   print "\nAll 17 pointed stars."
 
257
   paws ( )
 
258
 
 
259
   print "\nNext we will see a lissajous-style animation."
 
260
   print "First run without animation mode."
 
261
   print "Second run with animation mode."
 
262
 
 
263
 
 
264
   anim = Animation2d ( initialize = init, calculations = [calc1, calc2],
 
265
                     update = incr, animation = 0, nsteps = 200 )
 
266
 
 
267
   g1 = Graph2d ( anim , plotter = pl)
 
268
   g1.plot ( )
 
269
   paws ( )
 
270
   anim.set (animation = 1)
 
271
   g1.plot ( )
 
272