[Top] [Prev] [Next] [Bottom]

3.2 Lines Objects

This class is not currently supported by PyNarcisse.


from lines import *
l1 = Lines ( <keylist>)


A Lines object contains the specifications for a set of disjoint lines. It has only keyword arguments, in the form ``keyword = <value>''. It has methods set and new, which function the same as the Curve methods by the same name (see Section "Description" on page 9). The following keywords arguments are allowed:

x0, y0, x1, y1, color, hide, width, type

These keywords are described in the next subsection.

Keyword Arguments

The following keyword arguments can be specified for a Lines object:

x0 = <sequence of floating point values>
y0 = <sequence of floating point values>
x1 = <sequence of floating point values>
y1 = <sequence of floating point values>
x0, y0, x1, and y1 can actually be scalars, but if arrays must match in size and shape. (x0[i], y0[i]) represents the starting point of the ith line, and (x1[i], y1[i]) represents its endpoint.
color = one of the legal values for PyGist (currently the only package supporting Lines). See gist.help for details.
hide = 0/1 (1 to hide this part of the graph)
width = width of the lines. 1.0 (pretty narrow) is the default. Successive values 2.0, 3.0, ... roughly represent width in pixels.
type = "solid", "dash", "dot", "dashdot", "dashdotdot", and "none" (in which case the lines will be plotted as characters).

Example 1

The first example draws a series of lines starting at a set of equally spaced points along the x axis and ending at a set of equally spaced points along the vertical line x = 49. Subsequent commands change the plot as explained in the comments.

from lines import *
# Set up the points along x axis:
x0 = arange(50, typecode = Float)
y0 = zeros(50, Float)
# Set up the points along the line x = 49:
x1 = 49 * ones(50, Float)
y1 = arange(50, typecode = Float)
# Instantiate the Lines object ly:
ly = Lines (x0 = x0 ,y0 = y0, x1 = x1, y1 = y1)
# Instantiate a graph2d containing ly, with
# (bottom) title "Just Lines":
g0 = Graph2d ( ly , titles = "Just Lines")
# Plot the graph:
g0.plot ()
# Set the color of the lines to red; note that Lines,
# like Curve, has a method set:
ly.set (color = "red")
# Change the title to reflect this:
g0.change (titles = "Lines colored red")
# Plot the new graph:
g0.plot ()
# Now make the lines wider and change their type:
ly.set (width=4.0,type="dashdotdot")
# Change the title to reflect this:
g0.change(titles = "Wide lines, dashdotdot style")
# Plot the new graph:
g0.plot ()

Note once again that the Graph2d object g0 contains a reference to ly; hence when we change some of the characteristics of ly, g0 will know about these changes.

Example 2

The second example is more complicated, but is worth studying. It uses two functions to compute the endpoints of the lines; let us examine these functions first. Note that function a2 assumes that the Numeric module's name space has been imported.

def a2 (lb, ub, n) :
return reshape (array ((n - 1) * spanz (lb, ub, n),
Float), (n-1, n-1))

This function takes what spanz returns (which is a sequence of n - 1 items, as we shall see below), concatenates it to itself n - 1 times (the ``*'' is not multiplication, but replication), turns it into an array, then reshapes it into a two-dimensional array n - 1 by n - 1.

The spanz function is as follows:

def spanz (lb, ub, n) :
if n < 3 : raise ValueError, \
"3rd argument must be at least 3"
c = 0.5 * (ub - lb) / (n - 1.0)
b = lb + c
a = (ub -c -b) / (n - 2.0)
return map (lambda x, A = a, B = b:
A * x + B, range (n - 1))

The spanz function divides the interval from lb to ub into n parts, such that the interior subintervals are of equal length, and the two end subintervals are each half of that length.and returns the sequence of n - 1 equally spaced points which divide it into these n parts. If you do not understand how this function works, we recommend it as an excellent exercise to learn about the application of the map function and the lambda operator in Python.1

Although this manual is not a Python text, it might be instructive to study the following version of function a2, which does the same thing as the above a2 and spanz together:

def a2 (lb, ub, n) :
return multiply.outer (ones (17, Float),
arange (n - 1, typecode = Float) * (ub - lb) / (n - 1) +
(ub - lb) / (2 * (n - 1)))

With the help of these two auxiliary functions, or just the latter one, if you prefer, the following code will draw the graph of an interesting seventeen-pointed star:

# Create the endpoints:
theta = a2 (0, 2*pi, 18)
x = cos (theta)
y = sin (theta)
from lines import *
# Instantiate the lines object:
ln = Lines (x0 = x, y0 = y, x1 = transpose (x),
y1 = transpose (y))
# Instantiate a Graph2d object containing ln, with the x
# and y axes in equal scale, and an informative title:
g1 = Graph2d (ln, xyequal = 1,
titles = "Seventeen pointed star")
# Plot the graph:
g1.plot ()

[Top] [Prev] [Next] [Bottom]


See the Python Library Reference, page 17, for a description of map. The Python Reference Manual, p. 29, describes lambda forms.

Copyright © 1997,Regents of the University of California. All rights reserved.