GPS Location Plot

Now the moment we've been waiting for, plotting GPS data. To be able to follow along and plot data, be sure to define the functions read_csv_file() and process_gps_data() as previously detailed and set the file name variable to point to your GPS data file. I've suppressed matplotlib responses so that the code is cleaner to follow.

>>> filename = 'GPS-2008-05-30-09-00-50.csv' >>> y = read_csv_file('../data/'+filename) >>> (lat, long, v, t, sats) = process_gps_data(y) »> px = (long-min(long))*NMI*60.0*cos(D2R*lat) »> py = (lat-min(lat))*NMI*60.0 >>> figure()

>>> plot(px, py, 'b', label='Cruising', linewidth=3) >>> title(filename[:-4]) >>> legend(loc='upper left') >>> xlabel('east-west (meters)')

Figure 1-1 shows the result, which is rather pleasing.

Matplotlib Gps
Figure 1-1. GPS data

We've used a substantial number of new functions, all part of the matplotlib package: plot(), grid(), xlabel(), legend(), and more. Most of them are self-explanatory:

• xlabel(string_value) and ylabel(string_value) will print a label on the x- and y-axis, respectively. title(string value) is used to print a caption above the graph. The string value in the title is the file name up to the end minus four characters (so as to not display ".csv"). This is done using string slicing with a negative value, which means "from the end."

• legend() prints the labels associated with the graph in a legend box. legend() is highly configurable (see help(legend) for details). The example plots the legend at the top-left corner.

• grid() plots the grid lines. You can control the behavior of the grid quite extensively.

• plot() requires additional explanation as it is the most versatile. The command plot(px, py, 'b', label='Cruising', linewidth=3) plots pxand pywith the color blue as specified by the character 'b'. The plot is labeled "Cruising" so later on, when we call the legend() function, the proper text will be associated with the data. Finally, we set the line width to 3.

• The function axis() controls the behavior of the graph axis. Normally, I don't call the axis() function because plot() does a decent job at selecting the right values. However, in this case, it's important to visualize the data properly, and that means to have both x- and y-axes with equal increments so the graph is true to the path depicted. This is achieved by calling axis('equal'). There are other values to control axis behavior as described by help(axis).

• Lastly, gca().axes.invert_xaxis() is a rather exotic addition. It stems from the way we like to view maps and directions. In longitude, increasing values are displayed from right to left. However, in mathematical graphs, increasing values are typically displayed from left to right. This function call instructs the x-axis to be incrementing from right to left, just like maps.

• When you're done preparing the graph, calling the show() function displays the output.

Matplotlib, which includes the preceding functions, is a comprehensive plotting package and will be explored in Chapter 6.

Was this article helpful?

+2 0

Post a comment