## Annotating the Graph

We'd like to add some more information to the GPS graph: we'd like to know where we've stopped and where we were speeding. For this we use the function find(), which is part of the PyLab package. find() returns an array of indices that satisfy the condition, in our case:

»> STANDING_KMH = 10.0 »> SPEEDING_KMH = 50.0 »> Istand = find(v < STANDING_KMH) »> Ispeed = find(v > SPEEDING_KMH)

»> Icruise = find((v >= STANDING_KMH) & (v <= SPEEDING_KMH))

We also calculate when we're cruising (i.e., not speeding nor standing) for future processing.

To annotate the graph with these points, we add another plot on top of our current plot, only this time we change the color of the plot, and we use symbols instead of a solid blue line. The combination 'sg' indicates a green square symbol (g for green, s for square); the combination 'or' indicates a red circle (r for red, o for circle). I suggest you use different symbols for standing and speeding, not just colors, because the graph might be printed on a monochrome printer. The function plot() supports an assortment of symbols and colors; consult with the interactive help for details. The values we plot are only those returned by the find() function.

>>> plot(px[Istand], py[Istand], 'sg', label='Standing') >>> plot(px[Ispeed], py[Ispeed], 'or', label='Speeding!') >>> legend(loc='upper left')

Figure 1-2 shows the outcome.

GPS-2008-05-30-09-00-50 300 200 100

east-west (meters)

Figure 1-2. GPS data with additional speed information

300 200 100

east-west (meters)

-100

Figure 1-2. GPS data with additional speed information

We'd also like to know the direction the car is going. To implement this, we'll use the text() function, which allows the writing of a string to an arbitrary location in the graph. So to add the text "Hi" at location (10, 10), issue the command text(l0, 10, 'Hi'). One of the nice features of the text() function is that you can rotate the text at an arbitrary angle. So to plot"Hi"atlocation(10,10)at45degrees,youissuetext(l0, 10, 'Hi', rotation=45). Our implementation of heading information involves rotating the text ">>>" at the angle the car is heading. We'll only do this ten times so as not to clutter the graph with ">" symbols. Calculating the direction the car is heading at a given point, i, is shown in Listing 1-9.

Listing 1-9. Calculating the Heading dx = px[i+l]-px[i] dy = py[i+i]-py[i] heading = arctan(dy/dx)

Instead of actually using the function arctan(dy/dx), we'll use the function arctan2(dy, dx). The benefits of using arctan2() over arctan() are twofold: 1) there's no division that might cause a divide-by-zero exception in case dx is zero, and 2) arctan2() preserves the angle from -180 degrees to 180 degrees, whereas arctan() produces values between 0 degrees and 180 degrees only. The following code adds the direction symbols:

>>> for i in range(o, len(v), len(v)/10-l): ... text(px[i], py[i], "»>", \

... rotation = arctan2(py[i+l]-py[i], -(px[i+l]-px[i]))/D2R, \

Figure 1-3 shows the resulting graph. Figure 1-3. GPS graph with heading