[Tutor] basics of passing arguments to make a graph

Alan Gauld alan.gauld at btinternet.com
Fri Mar 23 10:14:53 CET 2007


"Che M" <pine508 at hotmail.com> wrote

> I've been trying to make basic plots (line graphs)
> using the pyplot module  in wxPython.

> class PlotPanel(wx.lib.plot.PlotCanvas):
>     def __init__(self, *args, **kwargs):
>          wx.lib.plot.PlotCanvas.__init__(self, *args, **kwargs)
>         self.Draw(self._drawGraph())
>
>    def _drawGraph(self):                                       #this
>         points = [(1,1), (3,4), (5,7), (7,14)]             #just 
> four points

This is the critical bit you need to change.
points is only defined inside this method,
you need to move the list into the class so
that you can access it as self.points. Also
you can set the list to a new value, add
new points etc.

To do that you should add new methods
to your class such as addPoints(), removePoints(),
replacePoints() etc. Also you should probably add
a list of points to your init method so you can pass
in an initial list - and set a default value of the
empty list. Like this:

     def __init__(self, points=[], *args, **kwargs):
          wx.lib.plot.PlotCanvas.__init__(self, *args, **kwargs)
         self.points = points
         self.Draw(self._drawGraph())

addPoints will be something very simple like:

     def addPoints(self, points):
          self.points += points

Now you just modify your drawGraph to use self.points:

>         m=[]
>         m.append(wx.lib.plot.PolyLine(points))

           m.append(wx.lib.plot.PolyLine(self.points))
etc.

> I get the sense that the idea is to pass a list of points
> to the drawGraph function.  Is that right?

If we were working with functions the answer would be yes,
but becausese we have a class we can store thelist as an
attribute of the class which allows us to manage the list
internally.

Another approach would be to create a points list class
and pass that to the drawGraph function. But in this case
the thing that cares about the points list is the PlotPanel
so it's reasonable that it should manage the points list.

>  Mostly because I don't understand argument passing
>  well at all (like the *args, **kwargs stuff is still mysterious
>  to me).

If you understand normal parameters/arguments then don't
worry too much about  *args/**kwargs. I've been using Python
for about 10 years and have only used those features in my
own code a couple of times. Most of the time you don't
need them. In this case so long as you keep them at the
end of your parameter list in init you can pretty much ignnore
them - they just pass any options you give to your class on to
the parent class.

In fact, I consider them sufficiently advanced that I don't even
discuss them in my tutor topic on functions. I probably should
introduce them somewhere I guess, but it will be in an
advanced topic somewhere...

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list