[Edu-sig] Graphing using Zelle's graphics.py

Kirby Urner urnerk at qwest.net
Mon Dec 15 05:06:09 EST 2003



John Zelle's graphics.py makes it easy for students to do graphics using Tk
from within IDLE.  

Plus IDLE is making things somewhat easier these days, e.g. there's the
'restart shell' feature that clears out the memory without necessitating a
reboot of IDLE.

Suppose I wanted a quick sine and cosine graphs using graphics.py.  

Here's one way that might look:

 >>> from zelle.graphics import *
 >>> import math

 >>> def graph(f, domain, thewindow):
     """
     Take a function, domain and target window,
     Return a graph
     """
         for x1,x2 in zip(domain[:-1], domain[1:]):
		seg = Line(  Point(x1,f(x1)),  Point(x2,f(x2))  )
		seg.draw(thewindow)

 >>> win = GraphWin('Graph',600,600)
 >>> win.setBackground('Blue')
 >>> win.setCoords(-10,-2,10,2)
		
 >>> domain = [i/100. for i in range(-1000,1000)]

 >>> graph(math.cos ,domain, win)
 >>> graph(math.sin ,domain, win)

 >>> win.close()

The setCoords method is especially convenient, as it lets the user decide
what the coordinate system will be.

Kirby





More information about the Edu-sig mailing list